aspose file tools*
The moose likes Java in General and the fly likes Email Address using java regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Email Address using java regular expression" Watch "Email Address using java regular expression" New topic
Author

Email Address using java regular expression

nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Hi team,

I am using the following pattern to validate my email address:-
static String EMAIL_MATCH_REG = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
which is working fine for
Valid email addresses:
niceandsimple@example.com OK
a.little.unusual@example.com OK
much."more\ unusual"@example.com How this one?
very.unusual."@".unusual.com@example.com How this one?
very."(),:;<>[]".VERY."very\\\ @\"very".unusual@cool.example.com How this one?

Please help me out.

Thanks,

Naveen Katoch
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

With this one
static String EMAIL_MATCH_REG = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-\\\\\"\\s]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

This one is working too
much."more\ unusual"@example.com OK now.

Thanks,

Naveen

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

how correct do you want to be? A legal email address can take lots of form, the spec is RFC822.

I use the Apace Common library to do it. No point in reinventing the wheel.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

Absolutely. You're just guessing at the rules, and if you do that then one day you're going to seriously annoy somebody by telling them their e-mail address isn't valid. If you really have to validate e-mail addresses (and you should try to design your system so you don't have to) then like Pat says, at least use code written by somebody who knows what they are doing.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

JavaMail with its InternetAddress is also an often-used library for parsing / validating email addresses.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Thank you so much for all your inputs. I appreciate it.

Naveen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Email Address using java regular expression