-4

Possible Duplicate:
What is the best regular expression for validating email addresses?

Hi I'm a newbie about the using of regular expression I have this regular expression to validate an mail but I don't understand the meaning.Someone could explain me?

mailPattern = Pattern.compile("[a-zA-Z_0-9]*[.[a-zA-Z_0-9]*]*@[a-zA-Z_0-9]*[.[a-zA-Z_0-9]*]*.[a-zA-Z]{2,3}");
Community
  • 1
  • 1
Mazzy
  • 13,354
  • 43
  • 126
  • 207
  • This is not a full pattern to validate an email address. take a look at the link I have put. – MByD Jan 04 '12 at 10:43
  • The answer specified in the link is useful, but not the best solution when using Java. Java/JavaMail already has a [built-in class](http://javamail.kenai.com/nonav/javadocs/javax/mail/internet/InternetAddress.html) that parses e-mail addresses based upon RFC822 specs. Using that is preferable to using any regex directly. – aroth Jan 04 '12 at 10:51

3 Answers3

4

If you're using Java, a much simpler option is to use JavaMail's InternetAddress class to validate the address for you:

public boolean isEmailValid(String email) {
    try {
        new InternetAddress(email);
        return true;
    }
    catch (Exception e) {
        return false;
    }
}

This will perform validation based upon RFC822. There's no need to try to come up with your own regex, really, or to copy regex's that others have come up with. Just use JavaMail's built-in utility, and move on to more meaningful things.

aroth
  • 54,026
  • 20
  • 135
  • 176
2

I recommend you the excellent web site http://www.regular-expressions.info/ to start learning regex

But your regex does not seems good. There is no need to have the asterix in a set.

here is the bad code : [.[a-zA-Z_0-9]*]

You need to use + instead of * in some part of your regex, etc.

validating email with regex is explained here (I recommend you to read it and use one of their regex instead of yours) : http://www.regular-expressions.info/email.html

Jerome Cance
  • 8,103
  • 12
  • 53
  • 106
  • 1
    `.name` has been around for a frikkin *decade*! But nooooo, every TLD is three characters at most. Oh, and there's no such thing as `+` allowed in the local part (and that's before resorting to the more exotic characters like `%`), right? And IDNs don't exist. The problems with that regex are far too numerous :( – Piskvor left the building Jan 04 '12 at 10:48
  • completely agree. I first try to write a post on all errors and finally edited my post to add the link to the website ;) – Jerome Cance Jan 04 '12 at 10:50
  • Oh yeah, *that* website. Note that the page you link to says this, essentially: "This regex validates all valid e-mail addresses (btw, when I say 'all e-mail addresses', I mean 'everything matched by this regex'; I don't give a damn that actual valid e-mail address not matching this exist)." Circular reasoning at its finest. – Piskvor left the building Jan 04 '12 at 10:52
-1

You can take a look at Java SE tutorial on regular expressions here.

Manish
  • 3,913
  • 2
  • 29
  • 45