Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Java email address validation (Regular Expression)

Posted on 2004-10-26
Medium Priority
Last Modified: 2010-03-31
I'm working on a java based email address validator using regular expressions, and it seems to be working for the most part, but I have noticed two things that don't work properly.

Here is my regular expression


The two things that don't work with it are that it allows spaces, which I'm not sure why.  Although if there are two spaces in a row, then it does say theres a problem.

Secondly, the last part of it, where I specify a-zA-Z {2,6} is for the ".com" part of the email, where the length has to be between 2, and 6.  However when i run an email address it doesn't check the length properly.  the email address test@this.a, and it fails it properly because of the length 1.  But if I use the email test@this.asdfgasd it passes it, even though its of length 8.

I know how to get around the space problem by using a tokenizer of " ", and checking the tokenCount.  But is there a way to do it in the regularexpression?  and how do I fix the length part for the final piece?

Question by:zaq
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 86

Accepted Solution

CEHJ earned 600 total points
ID: 12414353
"." will match a space. It should be "\\."


    System.out.println(" ".matches("\\."));
    System.out.println(" ".matches("."));

Why don't you use an off-the-shelf RE?
LVL 86

Expert Comment

ID: 12414422
>>Why don't you use an off-the-shelf RE?

btw, i consider myself pretty strong with REs, but wouldn't dream of concocting my own email RE. Try

final String EMAIL_RE = "^[A-Za-z0-9](([_\\.\\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\\.\\-]?[a-zA-Z0-9]+)*)\\.([A-Za-z]{2,})$";

Author Comment

ID: 12414432
i looked online, and this expression was from a php site.  all I did was converted it to java, and aparently incorrectly at that.

seems my understanding of regular expressions was incorrect.

Changing it to: ^([_a-zA-Z0-9-]+)(\\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+)(\\.[a-zA-Z0-9-]+)*(\\.[a-zA-Z]{2,6})$ made things work fine.  So the escape character of \\ in front of the . cleared up the space issue, and the count.  

LVL 86

Expert Comment

ID: 12414468

>>i looked online, and this expression was from a php site

Ah OK - it lost something in translation ;-)

Author Comment

ID: 12414480
yep.  my translation was just bad.

Thanks again for the help.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question