Take the 2-minute tour ×

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

String text = "gandalf_storm@mymail.com"; 
String regex = "(\\w+)@{1}(\\w+){2,}\\.{1}\\w{2,4}";

This however works with following cases :


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 :

gandalf.storm@mymail.com gandalf.storm@mydomain.me.uk gandalf.storm@mysubdomain.mydomain.me.uk gandalf.storm@mysubdomain.mysubdomain.mydomain.me.uk etc as many subdomains


gandalf.storm@mymail.com gandalf2storm@mydomain.me.uk gandalf_storm@mysubdomain.mydomain.me.uk gandalfstorm@mysubdomain.mysubdomain.mydomain.me.uk

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

share|improve this question
The {1} is redundant. –  Bart Kiers Apr 23 '10 at 8:53
noted thank you –  Gandalf StormCrow Apr 23 '10 at 9:01
add comment

4 Answers

up vote 2 down vote accepted

This question has been asked many, many times before here on SO. Here's why you don't want to use regexes to parse email addresses. Note please that that monster of a regex doesn't even handle comments.

share|improve this answer
I've just found suitable example to learn regex of course I'll not parse my emails with it, I'm just trying to learn regex by trying some examples –  Gandalf StormCrow Apr 23 '10 at 8:52
if you are trying to learn regex, that start with a subject which regex can be used on. email addresses is not one of those subjects. –  Sam Holder Apr 23 '10 at 8:54
@Sam Holder what are the subjects on which regex can be used on, from your expirience? –  Gandalf StormCrow Apr 23 '10 at 8:57
(...In general you don't need to handle comments as that's part of the RFC822 header format rather than part of the e-mail address as such. However, yes, this kind of regex ‘validation’ is a terrible idea.) –  bobince Apr 23 '10 at 9:01
@bobince do you have some other examples where to use regex?learning is the point here not using, of course I'm not going to validate it with regex, checking if it contains @ is enough for me. –  Gandalf StormCrow Apr 23 '10 at 9:03
show 3 more comments

see this question. Particularly the answer from Good Person. This article has some code for validating an email address.

share|improve this answer
add comment

The regex you use is very restrictive :

  • Using the \w character class before the @ does not allow the . character, which explains why gandalf.storm does not match
  • In the domain part of the regex, you only allow two "words" separated with a . character, which excludes "mysubdomain.mydomain.net"

You should try to fix these to match your more complicated examples.

As a side note, when you want to match a single character, the {1} part is not mandatory.

share|improve this answer
add comment

To answer your question, as you are learning.

The problem with your regex not matching with the first lot is partly because the part before the @ does not allow the '.' character. changing to this:

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

should allow gandalf.storm@mymail.com, because the [\\w.]+ says any character in the group '\w' (any character) or '.' (does not need to be escaped when part of a group, actually means a dot) 1 or more times

This might give you enough of a help to be able to figure the rest out on your own. after all that is the point of learning :)

I tested this at http://www.regexplanet.com/simple/index.html which uses the java library for the engine.

share|improve this answer
add comment

Your Answer


