Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for prefix exclusion

Tags:

regex

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?

like image 606
Tom Avatar asked Jun 08 '11 16:06

Tom


2 Answers

You want a negative lookbehind if your regex supports it, like (?<!@)gmail\.com and add \bs to avoid matching foogmail.comz, like: (?<!@)\bgmail\.com\b

like image 123
Qtax Avatar answered Sep 28 '22 03:09

Qtax


[^@\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.

like image 38
Tim Pietzcker Avatar answered Sep 28 '22 03:09

Tim Pietzcker