Search code examples
javascriptjavaregexgmail

Regex For Validating Email + Whitelisting Characters


I am new to regex and am trying to exclude emails that have special characters with the exception of the dot/period (.) and (@) from being included in my output. Is there something I may be doing wrong perhaps here? Here is what I have:

([a-zA-Z0-9]+)([\.{1}])?([a-zA-Z0-9]+)\@(?:gmail|GMAIL)([\.])(?:com|COM)

My text below is:

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

I am trying to match emails that look like [email protected] in my output, and exclude emails that are similar to [email protected] or any others that have special characters that are not (.) or (@). Please let me know if you can provide any help, thank you!

EDIT: To clarify, how would I grab all of the emails below EXCEPT the first line ([email protected])?


Solution

  • One way would be as demonstrated below. In the Regular Expression (regex) used below within the String#matches() method does contain some limits to what is acceptable as a valid E-Mail address which of course can be changed. They are as follows:

    • First Group: ([a-z]{1,10}\\.)? - The first portion of the E-Mail address Username may or may not contain a Start String consisting of all lower letter case (a to z) alphabetic characters from 1 to 10 characters in length and must be followed by a period (.) character. In this particular regex, this is considered optional in a valid E-Mail Address.

      If you want to make this Start String an actual requirement then simply remove the question mark (?). If you want to change the number of characters limits then change the values of 1 and or 10 (1 being the minimum allowed and 10 being the maximum allowed). If you don't can about a maximum then just remove the 10 but leave the comma ({1,}). If you want to also allow numbers then also place a number range within the Square Brackets, for example: ([a-z0-9]{1,10}\\.)?.

    • Second Group: ([a-z]){2,10} - The second portion to the Username of the E-Mail Address is mandatory and must consist of only lower letter case alphabetic characters from a to z. The can only be a minimum of 2 characters in this name and a maximum of 10 character. These limits can of course be changed to suit your needs (read the bullet above to find out how).

    • \\@ - The At Sign is mandatory and must be provided to be a valid E-Mail Address. Only one can be within an E-Mail Address and must be following by a domain name (see next bullet).

    • Third Group: (gmail.com|ymail.com|yahoo.com|hotmail.com) - The allowable Domain Names that can be within a valid E-Mail Address. Any domain name other than any one of those listed will be considered an Invalid E-Mail Address to the regex provided. You can add more domains names if you like as long as each is separated with the Pipe ( | ) character (regex OR).

    // However way you want to fill an String[] array of E-Mail Adresses:
    String[] addresses = {"[email protected]", "[email protected]",
                          "[email protected]", "[email protected]",
                          "[email protected]", "[email protected]"};
    
    for (String addy : addresses) {
        if (addy.matches("([a-z]{1,10}\\.)?([a-z]){2,10}\\@(gmail.com|ymail.com|yahoo.com|hotmail.com)")) {
            System.out.printf("E-Mail Address:  %-28s is Valid!%n", addy);
        }
        else {
            System.out.printf("E-Mail Address:  %-28s is NOT Valid!%n", addy);
        }
    }
    

    Console window should display:

    E-Mail Address:  [email protected]            is NOT Valid!
    E-Mail Address:  [email protected]        is Valid!
    E-Mail Address:  [email protected]    is Valid!
    E-Mail Address:  [email protected]     is Valid!
    E-Mail Address:  [email protected]    is Valid!
    E-Mail Address:  [email protected]     is Valid!