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
A word boundary, in most regex dialects, is a position between \w and \W (non-word char), or at the beginning or end of a string if it begins or ends (respectively) with a word character ( [0-9A-Za-z_] ). So, in the string "-12" , it would match before the 1 or after the 2.
The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point. Inside the lookahead, we have the trivial regex u. Positive lookahead works just the same.
Regular Expressions (Regex) Regular Expression, or regex or regexp in short, is extremely and amazingly powerful in searching and manipulating text strings, particularly in processing text files. One line of regex can easily replace several dozen lines of programming codes.
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
q
not 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: "aq
not followed by au
". It means: "aq
followed by a character that is not au
". It does not match theq
in the stringIraq
. It does match theq
and the space after theq
in 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)
.
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