Reputation: 763
I am trying to write a regex to match emails such that it rejects two email addresses [email protected]
and [email protected]
and match all other email addresses from a specific domain. So far, I have made the following pattern:
^([email protected]|[email protected])|(A-Z0-9)[A-Z0-9._%+-][email protected]$
So far, it is able to reject the main email addresses I want to block, but it accepts all other strings through this. As an alternative, I wrote the following regex:
^(?!test|tes2)[A-Z0-9._%+-][email protected]$
However, this rejects all patterns that start with either test or tes2 exclusively.
What is a better regular expression to get this scenario achieved?
Upvotes: 2
Views: 353
Reputation: 627292
"Fixing" your regex, you need to make sure you also test for @
inside the lookahead patterns to match test
and tes2
as full user names:
^(?!test@|tes2@)[A-Za-z0-9._%+-]+@testdomain\.com$
^ ^
Remember to escape a dot, too. See the regex demo.
However, you might want to be less restrictive on the user name part, and just use \S+
instead of your character class:
^(?!test@|tes2@)\S+@testdomain\.com$
^^^
See another regex demo. The \S+
pattern matches any 1 or more chars other than whitespace characters.
Upvotes: 1