Regex to match a whole string only if it lacks a given substring/suffix

Posted by Ivan Krechetov on Stack Overflow See other posts from Stack Overflow or by Ivan Krechetov
Published on 2009-12-28T10:13:46Z Indexed on 2011/01/06 10:53 UTC
Read the original article Hit count: 706

Filed under:
|

I've searched for questions like this, but all the cases I found were solved in a problem-specific manner, like using !g in vi to negate the regex matches, or matching other things, without a regex negation.

Thus, I'm interested in a “pure” solution to this:

Having a set of strings I need to filter them with a regular expression matcher so that it only leaves (matches) the strings lacking a given substring. For example, filtering out "Foo" in:

Boo
Foo
Bar
FooBar
BooFooBar
Baz

Would result in:

Boo
Bar
Baz

I tried constructing it with negative look aheads/behinds (?!regex)/(?<!regex), but couldn't figure it out. Is that even possible?

© Stack Overflow or respective owner

Related posts about regex

Related posts about regex-negation