22

[^abc] Any single character except: a, b, or c

But how can I make regex for any characters except sequence abc

So, something like that

"Hello abc awesome world".scan /[^(abc)]+/

Will return "Hello " and " awesome world".

PS: And it is not about splitting the string

fl00r
  • 82,987
  • 33
  • 217
  • 237

1 Answers1

26

This is called lookaround, in your case you'll want to use negative lookahead. I'm not sure about the exact syntax in Ruby, but something along (?!abc) might work. Note that the lookaround doesn't consume any input, so you'll need to have this followed by any pattern that you do want to match. Perhaps (?:(?!abc).)+ is what you're looking for?

krlmlr
  • 25,056
  • 14
  • 120
  • 217
  • 2
    Saved me muchos time. Was doing `(?!abc).+`, which did the negative lookahead for a whole sequence of characters. This one appears to do it on a character by character basis, which didn't even occur to me. Now all is well and happy. Gracias! – user1630830 Sep 20 '13 at 19:33