Search code examples
regexgmailspam

No period in first part of regular expression


This is what I'm currently working with: ((?i)(\w|^){0,25}[0-9]{3})[^\.]*@(gmail)\.com

What I'm attempting to do is block any email that is any amount of characters but with 3 numbers trailing the characters.

This works. HOWEVER, when Google creates a username for people, it usually chooses firstname.lastname###@gmail.com. I don't want an email with a period before the @gmail.com to be included.

I have played and played with this expression, and I can't get it. So for example [email protected], the expression is tagging everything after the period. I need for the regex to check the ENTIRE email and check to see if it follows the expression. I know there is this tidbit ^[^\.]*$ but I have no idea where to put it.


Solution

  • You could match 0-25 word characters followed by 3 digits \w{0,25}[0-9]{3} and use anchors to assert the start ^ and the end $ of the string.

    ^\w{0,25}[0-9]{3}@gmail\.com$
    

    Regex demo

    If you want to make use of the negated character class [^ you could match 0-25 times matching any char except a whitespace char, @ or a dot followed by 3 digits using [^\s@.]{0,25}[0-9]{3}

    ^[^\s@.]{0,25}[0-9]{3}@gmail\.com$
    

    Regex demo