Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 (notmyemail@sodifh.com)
John Smith (notmyemail@SODI.com)
John Smith (jsmit34@gmail.com)
John Smith (myRealEmail@mymail.com)

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading