Is it possible to use negative matches within gsub expressions?
I want to replace strings starting by hello
except those starting by hello Pe
As Michael told you you need a negative lookahead.
For your example is something like:
my_string.gsub(/^hello(?! peter)( .*|$)/i, '')
This will replace in cases like:
"hello"
"hello Mom"
"hello "
"hello Mom and Dad"
And will ignore things like:
"hello Peter"
"hello peter"
"hellomom"
"hello peter and tom"
Sounds like you want a negative lookahead:
>> "hello foo".gsub(/hello (?!peter)/, 'lala ') #=> "lala foo"
>> "hello peter".gsub(/hello (?!peter)/, 'lala ') #=> "hello peter"