Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Email Address using java regular expression

 
nav katoch
Ranch Hand
Posts: 246
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 246
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaMail with its InternetAddress is also an often-used library for parsing / validating email addresses.
 
nav katoch
Ranch Hand
Posts: 246
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for all your inputs. I appreciate it.

Naveen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic