Your Ad Here

Saturday, April 5, 2008

Java regular expression email validations

JavaRegxEmailValidations.java

1    import java.util.regex.Matcher;
2
import java.util.regex.Pattern;
3
import java.util.regex.PatternSyntaxException;
4
5
/**
6 * Created by IntelliJ IDEA.
7 * User: Ishara Samantha
8 * Date: Apr 6, 2008
9 * Time: 10:34:06 AM
10 * To change this template use File | Settings | File Templates.
11 */

12
public class JavaRegxEmailValidations
13 {
14
15
// \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
16
//
17
// Options: case insensitive
18
//
19
// Assert position at a word boundary «\b»
20
// Match a single character present in the list below «[A-Z0-9._%+-]+»
21
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
22
// A character in the range between “A” and “Z” «A-Z»
23
// A character in the range between “0” and “9” «0-9»
24
// One of the characters “._%” «._%»
25
// The character “+” «+»
26
// The character “-” «-»
27
// Match the character “@” literally «@»
28
// Match a single character present in the list below «[A-Z0-9.-]+»
29
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
30
// A character in the range between “A” and “Z” «A-Z»
31
// A character in the range between “0” and “9” «0-9»
32
// The character “.” «.»
33
// The character “-” «-»
34
// Match the character “.” literally «\.»
35
// Match a single character in the range between “A” and “Z” «[A-Z]{2,4}»
36
// Between 2 and 4 times, as many times as possible, giving back as needed (greedy) «{2,4}»
37
// Assert position at a word boundary «\b»
38
39
public static void main(String[] args)
40 {
41 System.out.println(
"emailValidation(\"ishara@gmail.com\") = " + emailValidation("ishara@gmail.com"));
42 System.out.println(
"emailValidation(\"ip@1.2.3.123\") = " + emailValidation("ip@1.2.3.123"));
43 System.out.println(
"emailValidation(\"pharaoh@egyptian.museum\") = " + emailValidation("pharaoh@egyptian.museum"));
44 System.out.println(
"emailValidation(\"john.doe+regexbuddy@gmail.com\") = " + emailValidation("john.doe+regexbuddy@gmail.com"));
45 System.out.println(
"emailValidation(\"Mike.O'Dell@ireland.com\") = " + emailValidation("Mike.O'Dell@ireland.com"));
46 System.out.println(
"emailValidation(\"\\\"Mike\\\\ O'Dell\\\"@ireland.com\") = " + emailValidation("\"Mike\\ O'Dell\"@ireland.com"));
47 System.out.println(
"emailValidation(\"IPguy@[1.2.3.4]\") = " + emailValidation("IPguy@[1.2.3.4]"));
48 System.out.println(
"emailValidation(\"ishara.samantha@gmail.com\") = " + emailValidation("ishara.samantha@gmail.com"));
49 System.out.println(
"emailValidation(\"ishara@ac.lk\") = " + emailValidation("ishara@ac.lk"));
50 System.out.println(
"emailValidation(\"1024x768@60Hz\") = " + emailValidation("1024x768@60Hz"));
51 System.out.println(
"emailValidation(\"not.a.valid.email\") = " + emailValidation("not.a.valid.email"));
52 System.out.println(
"emailValidation(\"not@valid.email\") = " + emailValidation("not@valid.email"));
53 System.out.println(
"emailValidation(\"john@aol...com\") = " + emailValidation("john@aol...com"));
54 System.out.println(
"emailValidation(\"Mike\\\\ O'Dell@ireland.com\") = " + emailValidation("Mike\\ O'Dell@ireland.com"));
55
56 }
57
58
private static boolean emailValidation(String email)
59 {
60
boolean foundMatch = false;
61
try {
62 Pattern regex = Pattern.compile(
"\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
63 Matcher regexMatcher = regex.matcher(email);
64 foundMatch = regexMatcher.find();
65 }
catch (PatternSyntaxException ex) {
66
// Syntax error in the regular expression
67 }
68
return foundMatch;
69 }
70 }
71
Email address
Use this version to seek out email addresses in random documents and texts.
Does not match email addresses using an IP address instead of a domain name.
Does not match email addresses on new-fangled top-level domains with more than 4 letters such as .museum. Including these increases the risk of false positives when applying the regex to random documents.
Requires the "case insensitive" option to be ON.
emailValidation("ishara@gmail.com") = true
emailValidation("ip@1.2.3.123") = false
emailValidation("pharaoh@egyptian.museum") = false
emailValidation("john.doe+regexbuddy@gmail.com") = true
emailValidation("Mike.O'Dell@ireland.com") = true
emailValidation("\"Mike\\ O'Dell\"@ireland.com") = false
emailValidation("IPguy@[1.2.3.4]") = false
emailValidation("ishara.samantha@gmail.com") = true
emailValidation("ishara@ac.lk") = true
emailValidation("1024x768@60Hz") = false
emailValidation("not.a.valid.email") = false
emailValidation("not@valid.email") = false
emailValidation("john@aol...com") = true
emailValidation("Mike\\ O'Dell@ireland.com") = true
More about email Validations as fallows
  1. Email address (anchored)
    Use this anchored version to check if a valid email address was entered.
    Does not match email addresses using an IP address instead of a domain name.
    Does not match email addresses on new-fangled top-level domains with more than 4 letters such as .museum.
    Requires the "case insensitive" option to be ON.
    • ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$
  2. Email address (anchored; no consecutive dots)
    Use this anchored version to check if a valid email address was entered.
    Improves on the original email address regex by excluding addresses with consecutive dots such as john@aol...com
    Does not match email addresses using an IP address instead of a domain name.
    Does not match email addresses on new-fangled top-level domains with more than 4 letters such as .museum. Including these increases the risk of false positives when applying the regex to random documents.
    Requires the "case insensitive" option to be ON.
    • ^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$
  3. Email address (no consecutive dots)
    Use this version to seek out email addresses in random documents and texts.
    Improves on the original email address regex by excluding addresses with consecutive dots such as john@aol...com
    Does not match email addresses using an IP address instead of a domain name.
    Does not match email addresses on new-fangled top-level domains with more than 4 letters such as .museum. Including these increases the risk of false positives when applying the regex to random documents.
    Requires the "case insensitive" option to be ON.
    • \b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b
  4. Email address (specific TLDs)
    Does not match email addresses using an IP address instead of a domain name.
    Matches all country code top level domains, and specific common top level domains.
    Requires the "case insensitive" option to be ON.
    • ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:com|org|net|gov|mil|biz|info|name|aero|biz|info|mobi|jobs|museum|[A-Z]{2})$
  5. Email address: RFC 2822
    This regular expression implements the official RFC 2822 standard for email addresses. Using this regular expression in actual applications is NOT recommended. It is shown to illustrate that with regular expressions there's always a trade-off between what's exact and what's practical.
    • (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
  6. Email address: RFC 2822 (simplified)
    Matches a normal email address. Does not check the top-level domain.
    Requires the "case insensitive" option to be ON.
    • [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
  7. Email address: RFC 2822 (specific TLDs)
    Matches all country code top level domains, and specific common top level domains.
    Requires the "case insensitive" option to be ON.
    • [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|name|aero|biz|info|mobi|jobs|museum)\b
 
 
 
 
 
 

No comments: