Home > Blockchain >  Filter emails that end with a digit while also filtering free emails
Filter emails that end with a digit while also filtering free emails

Time:08-20

I've created a regex to filter some email domains from my form:

/\@(?!(hotmail|msn)\.)/

This filters emails with domains of hotmail and msn successfully.

Now, I also want to optionally filter any emails that end with a digit. I created optional groupings. Group 1 checks for a digit at the end of the string before '@'. Group 2 checks for the domain.

Example of emails that are not allowed:

  1. [email protected]
  2. [email protected]
  3. [email protected]

Example of emails that are allowed:

  1. [email protected]
  2. [email protected]
/(?:(?!(\d \b)))?\@(?:(?!(hotmail|msn)\.))/

This filters emails with domains of hotmail and msn successfully, but does not filter an example like [email protected]

What is the issue?

CodePudding user response:

You could write the pattern as:

[^@\s\d] @(?!(?:hotmail|msn)\.)[^@\s] 

Explanation

  • [^@\s\d] Match 1 chars other than a whitespace char
  • @ Match literally
  • (?!(?:hotmail|msn)\.) Negative lookahead, assert not hotmail. or msn. directly to the right of the current position
  • [^@\s] Match 1 chars other than @ or a whitespace char

Regex demo

  • Related