Regex to exclude prefix

I am trying to extract gmail.com from the aisle where I want only those line matches that do not start with @.

Example: abc@gmail.com (does not match this); www.gmail.com (corresponding to this)

I tried the following: (?!@)gmail\.combut it did not work. This corresponds to both the cases highlighted in the example above. Any suggestions?

+3
source share
3 answers

You want to have a negative lookbehind if your regular expression supports it, for example, (?<!@)gmail\.comand adds \bto avoid matching foogmail.comz, for example:(?<!@)\bgmail\.com\b

+5
source
[^@\s]*(?<!@)\bgmail\.com\b

Assuming you want to find lines in longer text, do not check for whole lines.

:

[^@\s]*     # match any number of non-@, non-space characters
(?<!@)      # assert that the previous character isn't an @
\b          # match a word boundary (so we don't match hogmail.com)
gmail\.com  # match gmail.com
\b          # match a word boundary

(?<!@) lookbehind , : gmail.com abc@gmail.com.

+3

Use this regex using negative lookbehind :

/^.*?(?<!@)gmail\.com$/
+1
source

All Articles