For example, a regular expression that matches a string that does not contain "foo" but does contain "bar". I do not have the luxury of using two expressions. I need to do it in one expression.
ex.:
"this is foo baz bar" - should not match
"this is foo baz" - should not match
"this is baz bar" - should match
I'm in Perl, fwiw
Use a negative lookahead assertion at the start to check for the string not contain any foo
substring.
^(?!.*?foo).*bar.*
OR
^(?!.*?\bfoo\b).*\bbar\b.*
DEMO
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