With PCRE, how can you construct an expression that will only match if a string is not found.
If I were using grep (which I'm not) I would want the -v option.
A more concrete example: I want my regexp to match iff string foo
is not in the string. So it would match bar
would but not foobar
.
Definition and Usage. The ?! n quantifier matches any string that is not followed by a specific string n.
Similarly, the negation variant of the character class is defined as "[^ ]" (with ^ within the square braces), it matches a single character which is not in the specified or set of possible characters. For example the regular expression [^abc] matches a single character except a or, b or, c.
We say that two regular expressions R and S are equivalent if they describe the same language. In other words, if L(R) = L(S) for two regular expressions R and S then R = S.
Okay, I have refined my regular expression based on the solution you came up with (which erroneously matches strings that start with 'test').
^((?!foo).)*$
This regular expression will match only strings that do not contain foo. The first lookahead will deny strings beginning with 'foo', and the second will make sure that foo isn't found elsewhere in the string.
Based on Daniel's answer, I think I've got something that works:
^(.(?!test))*$
The key is that you need to make the negative assertion on every character in the string
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