E-Mail validation with Java and regular expressions (regex)

Submitted by rex on Mon, 06/08/2009 - 23:53

With a few lines of code you can check if an E-Mail address ist valid or not. The most import part ist our pattern:

String pattern = "([a-z0-9_.-]+@[a-z0-9-]+\\.[a-z]{2,6})";


import java.util.regex.*;

public class EmailCheck {

public static void main(String[] args) {

String eMail = "youremailyoursite.com";

String pattern = "([a-z0-9_.-]+@[a-z0-9-]+\\.[a-z]{2,6})";
if(eMail.matches(pattern)){
System.out.println("Your E-Mail address is valid");
}else{
System.out.println("You entered a not valid E-Mail address");
}

}

}