• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex Creation  RSS feed

 
Roopesh Rana
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi All,

I want to allow "+" sign in email address. for this I am using "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$" this regex, but if email id have "+" sing this is retiring false.



Please help "ryohei.j.okabe+1@gmail.com" will be valid email id.

Thanks
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have any + in highlighted part:
^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roopesh Rana wrote:I want to allow "+" sign in email address. for this I am using "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$" this regex, but if email id have "+" sing this is retiring false.

Right, well the first thing you need is a definitive statement in English (or your native language) of what an e-mail address is.

And for that, you need to go to the RFC for it (RFC 822 - there is an explanation of it here, which looks quite good; but the official one is here (and be prepared for a LONG read)).

However, since I've never read either, I can't tell you whether your regex is correct or not - although there are several on the 'Net that you could compare with. Just Google "email address regex".
I have to admit to never having seen an email name with a '+' sign in it though.

Another way of looking at it might be to treat an email address as:
  <name> + "@" + <domain>
and split that monster regex of yours into two smaller ones.
Have a look at the String.split() method for how you might do that.

HIH

Winston
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$

I assume you are using double backslashes because you putting this in a Java String. But the + doesn't need to be escaped inside square brackets.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E-mail address validation with a regular expression looks deceptively simple. A partiall RFC-822 compliant regex, which isn't fully compliant because it can't be, can be found here: http://ex-parrot.com/~pdw/Mail-RFC822-Address.html.
I'd recommend against going down this road.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, regarding ... [_A-Za-z0-9-\\+] ... it is recommend that the hypen be the first or last character of the character class (even though it does work here). Hypens at locations where it does not make sense is kinda ambiguous. Some regex engines will treat them as literals, while others will treat them as an error condition.

Henry
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that you've already found out that people get really annoyed when a computer system tells them that their perfectly valid e-mail address is not a valid e-mail address. It's bad for business when you do that. But I think your solution is wrong; you've just corrected one bug in your regex and you don't know whether there are others.

My suggestion would be to not validate e-mail addresses at all. If somebody wants to get e-mail from you then they will try to provide a correct e-mail address. You can assist them (and yourself) by sending a message to that address, but trying in advance to determine whether the mail will get there and be read is pointless.
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:... and you don't know whether there are others...


Which there are. A lot of others. Have a look at Email address and scroll down to the "Examples" section which will show you how far off you are.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!