Except or exclude authorized email domain

101 Views Asked by At

I have follow this comment and I've change a few adjustments to

\w+[-.w]*@(?!.+(com|co.kr|net|org))\w[-.\w]*\.+\w{2,4}$

for match any domain, but except ".com" , ".net" , "co.kr" , ".org" and contents in email body it's working fine. image_1 image_2

But if there is ".com" , ".net" , ".org" and "co.kr" before another domain. Or there is any symbol ( = , + ) it cannot match in this below. image_3

Where do I change to fix this?

2

There are 2 best solutions below

0
On BEST ANSWER

Today I've adjust regex completed. Exclude all desired domain (".com" , ".net" , ".org" and "co.kr") and another contents in email body.

(?i)[\w.+\-]{0,50}@[\w.+\-]{0,50}(?!.+(com|net|org|co.kr)).\w{2,5}\.\w{2,5}

The result is perfect. img_complete

1
On

You are not getting a match in the last examples because you want to assert that the .com .net .co.kr .org is at the end of the string using $ in the lookahead.

Your last example with the = sign in image_3 does not match as there is no @ sign in:

Lorem Ipsum is a long established. abcdxyz=network.0.0.0.clients.server.com.test

As there are no spaces in the match that you want, you can change the .+ to \S* to not cross matching spaces.

To match .com with the leading dot, you can add that to the pattern before starting the group.

Note that starting the match with \w+ could still get a partial match if there is a non word character preceding it, and using \w might limit the range of addresses that you actually want to match.

To prevent that, you can start the pattern with (?<!\S) but that has limited support.

\w+[-.w]*@(?!\S*\.(?:com|co\.kr|net|org)$)\w[-.\w]*\.\w{2,4}$

See a regex demo.