optional
optional

Reputation: 2782

Regular Expression should not match other prefixes

if for instance I have these words

and the task is to find all occurences of john=14.
I came up with the following regular expression: .*=[^14].*\n which matches every string without a leading 1 after the equal sign.
However, I want to exactly match only john=14 in this example (and also for permutations of this example). It doesn't matter if there are one or more john=14. I thought about negation of the regular expression, such that I want to find every string that isn't equal to the one I want to find but I had a problem with the regular expression ([^\bjohn\b=14]\n).

Any help would be appreciated :)!

Upvotes: 1

Views: 62

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174844

You need to use negative lookahead.

^(?!john=14$).*

Negative lookahead at the start asserts that the string going to be matched won't contain the exact john=14 string. If yes then match all the chars.

or

^(?!.*=14$).*

Upvotes: 1

Related Questions