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

regular expression affected by everything around it preventing a match

I have a simple regular expression for emails that I want to use, so it matches test@test.com for example, I don’t care about what’s around the email, except for one case when it starts with a * I don’t want the email to match in that case only. The regex I have does this but prevents other matches, for example, test>test@test.com it should match test@test.com in this case, but it doesn’t, I’ve done extensive research I can’t seem to figure it out, it should ONLY NOT match if there is a * in front of the email, no other case, from what I understand my current regex should do that.

((^[^\*][a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]+\D)$)

should match what is bolded

  • test@test.com
  • test@test.com.com
  • test>test@test.com<test

shouldn’t match

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

  • *test@test.com

>Solution :

What you could do is to make sure the character before the email is not a * using negative lookbehinds:

(?<!\*)\b[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z]+)+

Here are the test cases


For more restricted tests you may also try (?<![*.+-])\b(?![.+-])[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z]+)+ which excludes

*abc.test@test.com
*abc-test@test.com
*abc+test@test.com

etc.

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