Search code examples
regexpcre

Match email with a-z and dots or plus


I am trying to match emails with gmail domain that have as name the following characters: a-z, plus|dot. So, in the following list of emails:

answer.me...charlie@gmail.com
answer.me...charlie@tsunami.org
godfrey+assclown@gmail.com
wisk+as+w+e+rain@gmail.com
atomic+sam@outlook.org
canceee@gmail.com
linkedrebel@gmail.com
nuclearimpact+empoly@gmail.com
clericly.ass@gmail.com
r@gmail.com

Should match:

answer.me...charlie@gmail.com
godfrey+assclown@gmail.com
wisk+as+w+e+rain@gmail.com
nuclearimpact+empoly@gmail.com
clericly.ass@gmail.com

I tried with:

^((?=.*[a-z])(?=.*[.+]).+)@(g(?:oogle)?mail\.com)$

But the dot in the list [.+] is matching the dot of the domain name, making match all domain names. If I remove the dot, it matches only emails with plus characters as name. Any solution for that problem?

Regex101: https://regex101.com/r/bKqzan/1


Solution

  • You may temper all dots with [^@]:

    ^(?=[^@]*[a-z])(?=[^@]*[.+])([^@]+)@(g(?:oogle)?mail\.com)$
    

    This way all lookahead restrictions are only applied to the part before @. A more efficient variation:

    ^(?=[^@a-z]*[a-z])(?=[^@.+]*[.+])([^@]+)@(g(?:oogle)?mail\.com)$
    

    See the regex demo.

    Details

    • ^ - staert of string
    • (?=[^@]*[a-z]) - a positive lookahead that requires a lowercase letter after any 0+ chars other than @
    • (?=[^@]*[.+]) - a positive lookahead that requires a plus or dot after any 0+ chars other than @
    • ([^@]+) - Group 1: 1+ chars other than @
    • @ - a @ char
    • (g(?:oogle)?mail\.com) - Group 2: g optionally followed with oogle and then mail.com
    • $ - end of string.