Link to home
Start Free TrialLog in
Avatar of pzozulka
pzozulka

asked on

Regex Email Address

We have the below regex for an email address, in our ASP.NET application (C#).

It allows up to a maximum of two periods in the domain name (for ex: alex@jj.alex.ca). How do I modify it to allow up to three periods (ex: alex@jj.alex.ca.us)?
^(\w[\w!\$&\*\-=\^`\|~#%\+/\?{}\.']*@[\-\w]+\.([a-zA-Z]\w*(\.\w+)?)\s*)(;\s*\w[\w!\$&\*\-=\^`\|~#%\+/\?{}\.']*@[\-\w]+\.([a-zA-Z]\w*(\.\w+)?)\s*)*$

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pzozulka
pzozulka

ASKER

Before you gave me your suggestion, I came up with the below. Is it correct? I'm new to regex.
^(\w[\w!\$&\*\-=\^`\|~#%\+/\?{}\.']*@[\-\w]+\.([\-\w]+\.)?([a-zA-Z]\w*(\.\w+)?)\s*)(;\s*\w[\w!\$&\*\-=\^`\|~#%\+/\?{}\.']*@[\-\w]+\.([\-\w]+\.)?([a-zA-Z]\w*(\.\w+)?)\s*)*$

Open in new window

Basically, I added ([\-\w]+\.)? after @[\-\w]+\.
It appears to work, yes. I do think that your pattern would be easier to read if you ditched all of the unnecessary escapes. Most of those that are unnecessary are the ones inside of characters classes (i.e. square brackets).
Thanks much.
P.S.

I should have said in the above:

...after the one that's already there at the end.
Like this?

(\w[\w!\$&\*\-=\^`\|~#%\+/\?{}\.']*@[\-\w]+\.([a-zA-Z]\w*(\.\w+)?)(\.\w+)?\s*)(;\s*\w[\w!\$&\*\-=\^`\|~#%\+/\?{}\.']*@[\-\w]+\.([a-zA-Z]\w*(\.\w+)?)(\.\w+)?\s*)*$
Just the second one; get rid of the first one that you added (i.e. the first one that's in bold).