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
- *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.