Java regular expression example

This Java regular expression example demonstrates regex match for email, website address, social security number, date, and sentences containing a particular word.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex {

public static Pattern pattern;
public static Matcher matcher;

/**
 * Prints the text that matches the given pattern
 * @param input array of string
 * @param regEx regular expression pattern
 * @param notes a description of the pattern
 */
public static void find(String input[], String regEx, String notes) {

    Pattern pattern = Pattern.compile(regEx);

    System.out.println(notes + ":");

    for (int i = 0; i < input.length; i++) {
            matcher = pattern.matcher(input[i]);
            if (matcher.find()) {
                    System.out.println(input[i]);
            }
    }
    System.out.println();

}

public static void main(String[] args) throws Exception {

    String[] lines = { 
                    "Good day!", 
                    "http://google.com/",
                    "sandy123@gmail.com", 
                    "09.13.1900", 
                    "Java regex example",
                    "223-99-3312", 
                    "03/23/1982",
                    "Jeremy is very happy today because the weather is nice!",
                    "http://www.yahoo.com", 
                    "http://www.albany.edu", 
                    "341-92-2341",
                    "800-234-1023", 
                    "ken908@yahoo.com", 
                    "03-23-1999",
                    "http://www.health.org"
                    };

    // Regular expression patterns
    String emailRegex = "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\\b";
    String websiteRegex = "https?://(www\\.)?[A-Za-z0-9]+\\.(com|org|edu|gov|us)/?.*";
    String ssnRegex = "^[0-9]{3}-[0-9]{2}-[0-9]{4}$";
    String dateRegex = "^(0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])[-/.][0-9]{4}$";
    String startWithGoodRegex = "^(Good).*";
    String endWithExampleRegex =".*(example)$";
    String happyRegex = ".*happy.*";

    // Finding text with each pattern
    find(lines, emailRegex, "Emails");
    find(lines, websiteRegex, "Websites");
    find(lines, ssnRegex, "Social Security Numbers");
    find(lines, dateRegex, "Dates");
    find(lines, startWithGoodRegex, "Sentence starts with 'Good'");
    find(lines, happyRegex, "Sentence contains 'happy'");
    find(lines, endWithExampleRegex, "Sentence ends with 'example'");    
}

}

The output:

    Emails:
    sandy123@gmail.com
    ken908@yahoo.com

    Websites:
    http://google.com/
    http://www.yahoo.com
    http://www.albany.edu
    http://www.health.org

    Social Security Numbers:
    223-99-3312
    341-92-2341

    Dates:
    09.13.1900
    03/23/1982
    03-23-1999

    Sentence starts with ‘Good’:
    Good day!

    Sentence contains ‘happy’:
    Jeremy is very happy today because the weather is nice!

    Sentence ends with ‘example’:
    Java regex example

Search within Codexpedia

Custom Search

Search the entire web

Custom Search