Problems with matching emails with regex

Posted by Gandalf StormCrow on Stack Overflow See other posts from Stack Overflow or by Gandalf StormCrow
Published on 2010-04-23T08:46:48Z Indexed on 2010/04/23 8:53 UTC
Read the original article Hit count: 506

Filed under:
|

Hello,

I'm trying to match an email address here is what I've come up with so far :

String text = "[email protected]"; 
String regex = "(\\w+)@{1}(\\w+){2,}\\.{1}\\w{2,4}";

This however works with following cases :

[email protected]
[email protected]
[email protected]

So it matches any alphanumeric character repeated once or more that comes before one @ followed by any alphanumeric character repeated at least two times(which is minimal characters for any domain name) followed by one .(dot) and followed by any alphanumeric character repeated at least 2 times and at most 4 times(because there are domains such as .us or .mobi).

This expression however does not work with emails such as :

[email protected] [email protected] [email protected] [email protected] etc as many subdomains

or

[email protected] [email protected] [email protected] [email protected]

I've just started to learn regex and I found interesting to try to solve problems such as these by using regex .. not partially but for each case, any help would be much appriciated. Thank you

© Stack Overflow or respective owner

Related posts about regex

Related posts about java