• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Regex Creation

 
Ranch Hand
Posts: 49
  • 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
 
Bartender
Posts: 2233
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • 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,})$
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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.
 
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.
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Marshal
Posts: 25812
69
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
Marshal
Posts: 25812
69
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.
 
Let's get him boys! We'll make him read this tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic