Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find 'word' not followed by a certain character

Tags:

regex

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 
like image 714
user836026 Avatar asked Jul 03 '15 08:07

user836026


People also ask

What is a word boundary regex?

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.

How do you use negative lookahead?

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.

What regex means?

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.


1 Answers

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 a u. 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: "a q not followed by a u". It means: "a q followed by a character that is not a u". It does not match the q in the string Iraq. It does match the q and the space after the q in Iraq is a country. Indeed: the space becomes part of the overall match, because it is the "character that is not a u" that is matched by the negated character class in the above regexp. If you want the regex to match the q, and only the q, in both strings, you need to use negative lookahead: q(?!u).

like image 155
Wiktor Stribiżew Avatar answered Sep 23 '22 02:09

Wiktor Stribiżew