What is the regular expression to search for word string that is not followed by the @ symbol?
For example:
mywordLLD OK
myword.dff OK
myword@ld Exclude
What is the regular expression to search for word string that is not followed by the @ symbol?
For example:
mywordLLD OK
myword.dff OK
myword@ld Exclude
The (?!@) negative look-ahead will make word match only if @ does not appear immediately after word:
word(?!@)
If you need to fail a match when a word is followed with a character/string somewhere to the right, you may use any of the three below
word(?!.*@) # Note this will require @ to be on the same line as word
(?s)word(?!.*@) # (except Ruby, where you need (?m)): This will check for @ anywhere...
word(?![\s\S]*@) # ... after word even if it is on the next line(s)
See demo
This regex matches word substring and (?!@) makes sure there is no @ right after it, and if it is there, the word is not returned as a match (i.e. the match fails).
From Regular-expressions.info:
Negative lookahead is indispensable if you want to match something not followed by something else. When explaining character classes, this tutorial explained why you cannot use a negated character class to match a
qnot followed by au. Negative lookahead provides the solution:q(?!u). The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point.
And on Character classes page:
It is important to remember that a negated character class still must match a character.
q[^u]does not mean: "aqnot followed by au". It means: "aqfollowed by a character that is not au". It does not match theqin the stringIraq. It does match theqand the space after theqin Iraq is a country. Indeed: the space becomes part of the overall match, because it is the "character that is not au" that is matched by the negated character class in the above regexp. If you want the regex to match theq, and only theq, in both strings, you need to use negative lookahead:q(?!u).