Regex to match a whole string only if it lacks a given substring/suffix [duplicate]
Asked Answered
P

1

8

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?

Pung answered 28/12, 2009 at 10:13 Comment(1)
Background: phpunit has --filter argument which accepts a regular expression for included test cases' names, but doesn't have an excluding analog. That's what made me ask this question, which is interesting by itself, regardless of phpunit command line arguments.Pung
V
19

Try this regular expression:

^(?:(?!Foo).)*$

This will consume one character at a time and test if there is no Foo ahead. The same can be done with a negative look-behind:

^(?:.(?<!Foo))*$

But you can also do the same without look-around assertions:

^(?:[^F]*|F(?:$|[^o].|o(?:$|[^o])))*$

This matches any character except F or an F that is either not followed by a o or if followed by an o not followed by another o.

Virg answered 28/12, 2009 at 10:17 Comment(3)
What does the 2nd ? in ^(?:(?!Foo).)*$ do ?Venusian
Got it its part of : (?<!pattern), +1Venusian
@gameover: It does the same like the first expression but just in the reverse order: Consume one character at a time and test if the last three consumed characters are not Foo.Virg

© 2022 - 2024 — McMap. All rights reserved.