using negative conditions within regular expressions

前端 未结 2 1205
悲哀的现实
悲哀的现实 2021-02-09 14:02

Is it possible to use negative matches within gsub expressions? I want to replace strings starting by hello except those starting by hello Pe

2条回答
  •  终归单人心
    2021-02-09 14:40

    Sounds like you want a negative lookahead:

    >> "hello foo".gsub(/hello (?!peter)/, 'lala ') #=> "lala foo"
    >> "hello peter".gsub(/hello (?!peter)/, 'lala ') #=> "hello peter"
    

提交回复
热议问题