Search code examples
regexregex-group

REGEX Select From Where Word NOT Found Difficulty


I need a regex to match phishing email names, where a specific name or word is NOT found.

For example...

John Smith ([email protected])
John Smith ([email protected])
John Smith ([email protected])
John Smith ([email protected])

Where the bottom email is the only legit email, and needs to be ignored.

I have tried similar to:

^(J|j)ohn.(S|s)mith.*\@^((?i)\bmymail\.com\b)*$

But this fails to select the 3 bad emails. Also tried similar to:

^(J|j)ohn.(S|s)mith.*@gmail\.com

but this method only removes the Gmail one.

How can I properly pattern match to remove all bad emails regardless of letter case, and to only allow one domain (in this case mymail.com) and no others?


Solution

  • You may use this regex with a negative look ahead condition

    ^[jJ]ohn.[sS]mith[^@]*@(?!mymail\.com).+
    

    RegEx Demo

    RegEx Details:

    • ^: Start
    • [jJ]ohn: Match John or john
    • .: Match any character
    • [sS]mith[: Match Smith or smith
    • [^@]*: Match 0 or kore of any character except @
    • @: Match a @
    • (?!mymail\.com): Negative lookahead to fail the match if we have mymail.com is at next position
    • .+: Match 1+ of any character