Datasheet

Method Description
int flags() Returns an integer containing the flags set when
the regular expression was compiled.
Matcher matcher(CharSequence input) Returns a Matcher to use for matching the pattern
against the specified input.
String pattern() Returns the regular expression that was used to
create the pattern.
String[] split(CharSequence input) Returns an array of strings after splitting the input
String[] split(CharSequence input,
into chunks using the regular expression as a
int limit)
separator. The limit can be used to limit how many
times the regular expression is matched. The match-
ing text does not get placed into the array. If
limit
is positive, the pattern will be applied at least “limit
minus 1” times. If
limit is 0, the pattern will be
applied as many times as it can, and trailing empty
strings are removed. If
limit is negative, the pat-
tern will be applied as many times as it can, and
trailing empty strings will be left in the array.
The Matcher Class
The Matcher class is used to use a pattern to compare to an input string, and perform a wide variety of
useful tasks. The
Matcher class provides the ability to get a variety of information such as where in the
string a pattern matched, replace a matching subset of the string with another string, and other useful
operations.
Method Description
static String Returns a string that is quoted with \Q and \E and can be used
quoteReplacement(String s) to match literally with other input.
Matcher appendReplacement First appends all characters up to a match to the string buffer,
(StringBuffer sb, String then replaces the matching text with replacement, then sets
replacement) the index to one position after the text matched to prepare for
the next call to this method. Use
appendTail to append the
rest of the input after the last match.
StringBuffer appendTail Appends the rest of the input sequence to the string buffer that
(StringBuffer sb) is passed in.
MatchResult asResult() Returns a reference to a MatchResult describing the matcher’s
state.
int end() Returns the index that is one past the ending position of the last
match.
66
Part I: Thinking Like a Java Developer
05_777106 ch01.qxp 11/28/06 10:43 PM Page 66