[^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
To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With