13.3.2. Compiling and Matching with Regular Expressions
Evaluating a regular expression can be compute intensive, and in many instances a single regular expression
will be used repeatedly. This can be addressed by compiling the regular expression once and using the result.
In addition, a single character sequence might be checked repeatedly against the same pattern to find multiple
matches, which can be done fastest by remembering some information about previous matches. To address
both these opportunities for optimization, the full model of using a regular expression is this:
- First you turn your regular expression string into a Pattern object that is the compiled version of the
pattern. - Next you ask the Pattern object for a Matcher object that applies that pattern to a particular
CharSequence (such as a String or StringBuilder). - Finally you ask the Matcher to perform operations on the sequence using the compiled pattern.
Or, expressed in code:
Pattern pat = Pattern.compile(regularExpression);
Matcher matcher = pat.matcher(sequence);
boolean foundMatch = matcher.find();
If you are only using a pattern once, or are only matching each string against that pattern once, you need not
actually deal with the intermediate objects. As you will see, there are convenience methods on Pattern for
matching without a Matcher, and methods that create their own Pattern and Matcher. These are easy to
use, but inefficient if you are using the same pattern multiple times, or matching against the same string with
the same pattern repeatedly.
The Pattern class has the following methods:
public static Patterncompile(String regex)tHRows
PatternSyntaxException
Compiles the given regular expression into a pattern.
public static Patterncompile(String regex, int flags)tHRows
PatternSyntaxException
Compiles the given regular expression into a pattern with the given flags. The
flags control how certain interesting cases are handled, as you will soon
learn.
public Stringpattern()
Returns the regular expression from which this pattern was compiled.
public intflags()
Returns this pattern's match flags.
public Matchermatcher(CharSequence input)