Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regexp: Any characters except sequence

Tags:

regex

ruby

[^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

like image 861
fl00r Avatar asked Mar 06 '12 12:03

fl00r


People also ask

How do you match a character except one?

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.

What does \+ mean in regex?

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 "(" .

What is difference [] and () in regex?

[] 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 .


1 Answers

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?

like image 156
krlmlr Avatar answered Nov 02 '22 04:11

krlmlr