Tech and Media Labs
This site uses cookies to improve the user experience.




Java Regex - Pattern (java.util.regex.Pattern)

Jakob Jenkov
Last update: 2014-12-17

The Java Pattern class (java.util.regex.Pattern), is the main access point of the Java regular expression API. Whenever you need to work with regular expressions in Java, you start with Java's Pattern class.

Working with regular expressions in Java is also sometimes referred to as pattern matching in Java. A regular expression is also sometimes referred to as a pattern (hence the name of the Java Pattern class). Thus, the term pattern matching in Java means matching a regular expression (pattern) against a text using Java.

The Java Pattern class can be used in two ways. You can use the Pattern.matches() method to quickly check if a text (String) matches a given regular expression. Or you can compile a Pattern instance using Pattern.compile() which can be used multiple times to match the regular expression against multiple texts. Both the Pattern.matches() and Pattern.compile() methods are covered below.

Pattern.matches()

The easiest way to check if a regular expression pattern matches a text is to use the static Pattern.matches() method. Here is a Pattern.matches() example in Java code:

import java.util.regex.Pattern;

public class PatternMatchesExample {

    public static void main(String[] args) {

        String text    =
            "This is the text to be searched " +
            "for occurrences of the pattern.";

        String pattern = ".*is.*";

        boolean matches = Pattern.matches(pattern, text);

        System.out.println("matches = " + matches);
    }
}

This Pattern.matches() example searches the string referenced by the text variable for an occurrence of the word "is", allowing zero or more characters to be present before and after the word (the two .* parts of the pattern).

The Pattern.matches() method is fine if you just need to check a pattern against a text a single time, and the default settings of the Pattern class are appropriate.

If you need to match for multiple occurrences, and even access the various matches, or just need non-default settings, you need to compile a Pattern instance using the Pattern.compile() method.

Pattern.compile()

If you need to match a text against a regular expression pattern more than one time, you need to create a Pattern instance using the Pattern.compile() method. Here is a Java Pattern.compile() example:

import java.util.regex.Pattern;

public class PatternCompileExample {

    public static void main(String[] args) {

        String text    =
                "This is the text to be searched " +
                "for occurrences of the http:// pattern.";

        String patternString = ".*http://.*";

        Pattern pattern = Pattern.compile(patternString);
    }
}

You can also use the Pattern.compile() method to compile a Pattern using special flags. Here is a Java Pattern.compile() example using special flags:

Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);

The Java Pattern class contains a list of flags (int constants) that you can use to make the Pattern matching behave in certain ways. The flag used above makes the pattern matching ignore the case of the text when matching. For more information of the flags you can use with the Java Pattern class, see the JavaDoc for Pattern .

Pattern.matcher()

Once you have obtained a Pattern instance, you can use that to obtain a Matcher instance. The Matcher instance is used to find matches of the pattern in texts. Here is an example of how to create a Matcher instance from a Pattern instance:

Matcher matcher = pattern.matcher(text);

The Matcher class has a matches() method that tests whether the pattern matches the text. Here is a full example of how to use the Matcher:

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

public class PatternMatcherExample {

    public static void main(String[] args) {

        String text    =
            "This is the text to be searched " +
            "for occurrences of the http:// pattern.";

        String patternString = ".*http://.*";

        Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);

        Matcher matcher = pattern.matcher(text);

        boolean matches = matcher.matches();

        System.out.println("matches = " + matches);
    }
}

The Matcher is very advanced, and allows you access to the matched parts of the text in a variety of ways. Too keep this text short, the Matcher covered in more detail in the text about the Java Matcher class.

Pattern.split()

The split() method in the Pattern class can split a text into an array of String's, using the regular expression (the pattern) as delimiter. Here is a Java Pattern.split() example:

import java.util.regex.Pattern;

public class PatternSplitExample {

    public static void main(String[] args) {
    
        String text = "A sep Text sep With sep Many sep Separators";
        
        String patternString = "sep";
        Pattern pattern = Pattern.compile(patternString);
        
        String[] split = pattern.split(text);
        
        System.out.println("split.length = " + split.length);
        
        for(String element : split){
            System.out.println("element = " + element);
        }
    }
}

This Pattern.split() example splits the text in the text variable into 5 separate strings. Each of these strings are included in the String array returned by the split() method. The parts of the text that matched as delimiters are not included in the returned String array.

Pattern.pattern()

The pattern() method of the Pattern class simply returns the pattern string (regular expression) that the Pattern instance was compiled from. Here is an example:

import java.util.regex.Pattern;

public class PatternPatternExample {

    public static void main(String[] args) {
    
        String patternString = "sep";
        Pattern pattern = Pattern.compile(patternString);
        
        String pattern2 = pattern.pattern();
    }
}

In this example the pattern2 variable will contain the value sep, which was the value the Pattern instance was compiled from.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC