Thursday, October 28, 2010

Pattern and Matcher

In addition to the regular expression methods that are available in the String class (see String Regular Expressions), there are two classes that are specifically user for regular expression matching.

  • java.util.regex.Pattern precompiles regular expressions so they can be executed more efficiently. It also has a few utility functions. This same pattern can be reused by many Matcher objects.
        Pattern pat = Pattern.compile(regexString);

  • java.util.regex.Matcher objects are created from a Pattern object and a subject string to scan. This class provides a full set of methods to do the sacnning.
        Matcher m = pat.matcher(subject);

Common methods

The following variables represent elements of their declared type in the prototypes below.

import java.util.regex.*;
. . .
boolean b; // may be used in if statement.
int i; // index or character position.
int g; // group number
int n; // number of groups
CharSequence cs; // effectively either String or StringBuffer
String s; // Subject string.
String regex; // Regular expression string.
StringBuffer sb; // Used to build up string from repeated scans.
Pattern p = Pattern.compile(regex); // Compiles regular expression into Pattern.
Matcher m = p.matcher(s); // Creates Matcher with subject s and Pattern p.


Creating a Pattern

p =
Creates Pattern based on regex. May throw PatternSyntaxException.

p =
Pattern.compile(regex, f);
As above. Flag f can be Pattern.CASE_INSENSITIVE, ....

Finding pattern matches

b =
True if pattern can be found in the subject string. First call starts trying at beginning of string. Subsequent calls start after last character previously matched, which makes it good for a while loop.

b =
True if pattern can match somewhere at or after position i.

b =
True if pattern matches entire subject string.

b =
Pattern.matches(regex, s);
As above, but less efficient if regex used more than once.

b =
True if pattern matches starting at first char.

Getting results of last pattern match. Corresponds to group 0, then entire match.

s =;
String which was matched.

i =
Index of first character of match.

i =
Index of last character plus 1 of match.

Getting group results of last match

s =;
String which was matched by group g.

i =
Index of first character of group g.

i =
Index of last character plus 1 of group g.

n =
Number of groups that were matched.


m =
Resets Matcher m so that next find starts at beginning.

m =
Resets Matcher m to match subject cs.

Replacing text

s =
Returns string which is subject with first match replaced by rep.

s =
Returns string with all matches replaced by rep.

Building replacement in a StringBuffer

m =
m.appendReplacement(sb, s);
When it matches, (1) everything skipped before the match is appended to the StringBuffer sb, then (2) it appends s, with "$n" replaced by group n.

sb =
Appends last part that m didn't match to StringBuffer. Useful after loop calling appendReplacement() to finish.

Other Resources

No comments:

Post a Comment