Reputation: 540
I am trying to extract gmail.com from a passage where I want only those string match that don't start with @.
Example: [email protected] (don't match this); www.gmail.com (match this)
I tried the following: (?!@)gmail\.com
but this did not work. This is matching both the cases highlighted in the example above. Any suggestions?
Upvotes: 9
Views: 17618
Reputation: 786091
Use this regular expression using negative lookbehind:
/^.*?(?<!@)gmail\.com$/
Upvotes: 2
Reputation: 33928
You want a negative lookbehind if your regex supports it, like (?<!@)gmail\.com
and add \b
s to avoid matching foogmail.comz
, like: (?<!@)\bgmail\.com\b
Upvotes: 14
Reputation: 336478
[^@\s]*(?<!@)\bgmail\.com\b
assuming you want to find strings in a longer text body, not validate entire strings.
Explanation:
[^@\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
On a first glance, the (?<!@)
lookbehind assertion appears unnecessary, but it isn't - otherwise the gmail.com
part of [email protected]
would match.
Upvotes: 9