Common regular expressions

Regular expressions get tricky and sometimes are hard to read. Be cautious of:

  • performance when instantiating Pattern
  • using String.matches(regex)
  • slight variations between languages

If you decide to use an expression below, since there are so many variations be sure to test it so it meets your requirements. There are many online regular expression testers you can use for testing.

Employer Identification Number FEIN regex

An employer identification number or FEIN or EIN is the corporate equivalent to a Social Security number. This regular expression will validate an employer identification number.

@Test
public void regex_tax_id_or_employer_identification_number () {

    String taxIdRegex = "^([07][1-7]|1[0-6]|2[0-7]|[35][0-9]|[468][0-8]|9[0-589])-?\d{7}$";

    assertTrue("45-4444444".matches(taxIdRegex));
    assertTrue("454444444".matches(taxIdRegex));

    assertFalse("98765432".matches(taxIdRegex));
}

HTML tag regex

In a prior example we showed how to escape HTML characters and below we will use a regular expression to validate if a string is a valid HTML element. An HTML element is a component or text that makes up a part of a HTML document. This text typically has an beginning tag and ending tag.

@Test
public void regex_html_tag () {

    String htmlRegex = "(\<(/?[^\>]+)\>)";

    assertTrue("<h2>".matches(htmlRegex));
    assertTrue("</h2>".matches(htmlRegex));
    assertTrue("<p>".matches(htmlRegex));
    assertTrue("<script type='text/javascript'>".matches(htmlRegex));
    assertTrue("<a href=''>".matches(htmlRegex));
    assertTrue("<img src=''>".matches(htmlRegex));

    assertFalse("* bullet point".matches(htmlRegex));
}

Validate url regex

This regular expression will validate a url with a regular expression.

@Test
public void regex_valid_url () {

    String urlRegex = "\b(https?|ftp|file|ldap)://"
            + "[-A-Za-z0-9+&@#/%?=~_|!:,.;]"
            + "*[-A-Za-z0-9+&@#/%=~_|]";

    assertTrue("http://www.leveluplunch.com".matches(urlRegex));

    assertTrue("http://www.leveluplunch.com/index.html".matches(urlRegex));

    assertTrue("http://leveluplunch.com".matches(urlRegex));

    assertTrue("https://www.leveluplunch.com/examples".matches(urlRegex));

    assertTrue("http://www.leveluplunch.com/examples?abc=123".matches(urlRegex));

    assertTrue("ldap://myhost:389".matches(urlRegex));

    assertTrue("ftp://user:password@host:port/URI?queryParameters".matches(urlRegex));

    assertFalse("www.leveluplunch.com".matches(urlRegex));
}

Number range regex

This snippet will use a regular expression to validate a range of numbers.

@Test
public void regex_number_range () {

    String numberRangeRegex = "\d{3}";

    assertTrue("123".matches(numberRangeRegex));

    assertFalse("1223".matches(numberRangeRegex));

    assertFalse("12".matches(numberRangeRegex));
}

Consecutive duplicated words regex

This regex will check if there is a series of words that are duplicated.

@Test
public void regex_consecutive_duplicated_words () {

    String duplicatedWordRegex = "\b(\w+)\s+\1\b";

    assertTrue("so so".matches(duplicatedWordRegex));

    assertFalse("so this, so this".matches(duplicatedWordRegex));
}

Phone number regex

This regular expression will validate the format of a phone number.

@Test
public void regex_phone_numbers () {

    String phoneNumberRegex = "^(\([2-9]|[2-9])"
            + "(\d{2}|\d{2}\))"
            + "(-|.|\s)?\d{3}(-|.|\s)?\d{4}$";

    assertTrue("223-123-1234".matches(phoneNumberRegex));

    assertTrue("800-123-1234".matches(phoneNumberRegex));

    assertTrue("(223) 123-1234".matches(phoneNumberRegex));

    assertTrue("(223)123-1234".matches(phoneNumberRegex));

    assertTrue("223.123.1234".matches(phoneNumberRegex));

    assertFalse("000-000-0000".matches(phoneNumberRegex));

    assertFalse("1-223-123-1234".matches(phoneNumberRegex));
}

Zip code regex

This regular expression will validate the format of a zip code.

@Test
public void regex_zip_code () {

    String zipCodeRegEx = "[0-9]{5}(\-?[0-9]{4})?$";

    assertTrue("53546".matches(zipCodeRegEx));
    assertTrue("53546-1234".matches(zipCodeRegEx));

    assertFalse("a53546".matches(zipCodeRegEx));
}

Email address regex

This snippet will show how to validate an email address with a regular expression.

@Test
public void regex_email_address () {

    String emailRegEx = "^[a-zA-Z0-9'._%+-]+@"
            + "(?:[a-zA-Z0-9-]+\.)"
            + "+[a-zA-Z]{2,4}$";

    assertTrue("[email protected]".matches(emailRegEx));

    assertTrue("[email protected]".matches(emailRegEx));

    assertTrue("[email protected]".matches(emailRegEx));

    assertTrue("[email protected]".matches(emailRegEx));

    assertTrue("[email protected]".matches(emailRegEx));

    assertFalse("f-last@leveluplunch".matches(emailRegEx));
}

Image file extension regex

This snippet will show how to validate image file extension with regular expression.

@Test
public void regex_image_file_names () {

    String fileImageTypes = "([^\s]+(?=\.(jpg|gif|png))\.\2)";

    assertTrue("/assets/images/leveluplunch.jpg".matches(fileImageTypes));

    assertTrue("/assets/assets/images/leveluplunch.gif".matches(fileImageTypes));

    assertTrue("/img/leveluplunch.png".matches(fileImageTypes));

    assertFalse("leveluplunch.mov".matches(fileImageTypes));
}