/abc(?!$)/
(?!$)
is a negative lookahead. It will look for any match of abc that is not directly followed by a $
(end of line)
Tested against
- abcddee (match)
- dddeeeabc (no match)
- adfassdfabcs (match)
- fabcddee (match)
applying it to your case:
ruby-1.9.2-p290 :007 > "aslkdjfabcalskdfjaabcaabc".gsub(/abc(?!$)/, 'xyz')
=> "aslkdjfxyzalskdfjaxyzaabc"
Related Contents:
- Regex to match Date
- Difference between \A \z and ^ $ in Ruby regular expressions
- How to match all occurrences of a regex
- How to match all occurrences of a regular expression in Ruby
- How do I get the match data for all occurrences of a Ruby regular expression in a string?
- How do I remove emoji from string
- Weird backslash substitution in Ruby
- Ruby regular expression using variable name
- Regex to split BBCode into pieces
- Valid email address regular expression? [duplicate]
- Extract a substring from a string in Ruby using a regular expression
- Confusion with Atomic Grouping – how it differs from the Grouping in regular expression of Ruby?
- Ruby Regular expression to match a url [duplicate]
- Ruby Email validation with regex
- Match a string against multiple patterns
- Strange, unexpected behavior (disappearing/changing values) when using Hash default value, e.g. Hash.new([])
- How to call shell commands from Ruby
- How do I use the conditional operator (? :) in Ruby?
- What is Ruby’s double-colon `::`?
- What is the Ruby (spaceship) operator?
- How to write a switch statement in Ruby
- Difference between “and” and && in Ruby?
- Ruby block and unparenthesized arguments
- How to debug Ruby scripts [closed]
- What is the difference between require_relative and require in Ruby?
- What is the difference or value of these block coding styles in Ruby?
- Best way to require all files from a directory in ruby?
- Ruby: What does ‘require: false’ in Gemfile mean?
- What does &. (ampersand dot) mean in Ruby?
- Debugging in ruby 1.9
- Ruby Style: How to check whether a nested hash element exists
- Ruby function to remove all white spaces?
- Difference between various variables scopes in ruby
- How does object_id assignment work?
- what’s different between each and collect method in Ruby [duplicate]
- Generating GUIDs in Ruby
- cannot load such file — script/rails : Getting this error while remote debugging through RubyMine
- How do I run a rake task from Capistrano?
- How to chunk an array in Ruby
- How do I temporarily redirect stderr in Ruby?
- How do I keep the delimiters when splitting a Ruby string?
- Unresolved specs during Gem::Specification.reset:
- How do I create a class instance from a string name in ruby?
- Reading the last n lines of a file in Ruby?
- How to get possibly overlapping matches in a string
- Ruby : How to write a gem? [closed]
- Ruby: how can I copy a variable without pointing to the same object?
- ERROR: While executing gem … (Gem::FilePermissionError)
- What does class_eval
- Understanding Ruby and OS I/O buffering