Name
Matcher
Synopsis
A
Matcher
objects
encapsulate a regular expression and a string of text (a
Pattern
and a
java.lang.CharSequence
) and defines methods for
matching the pattern to the text in several different ways, for
obtaining details about pattern matches, and for doing
search-and-replace operations on the text. Matcher has no public
constructor. Obtain a Matcher
by passing the
character sequence to be matched to the matcher( )
method of the desired Pattern
object. You can also
reuse an existing Matcher
object with a new
character sequence (but the same Pattern
) by
passing a new CharSequence
to the
matcher’s reset( )
method. In
Java 5.0, you can use a new Pattern
object on the
current character sequence with the usePattern( )
method.
Once you have created or reset a Matcher
, there
are three types of comparisons you can perform between the regular
expression and the character sequence. All three comparisons operate
on the current region of the character sequence.
By default, this region is the entire sequence. In Java 5.0, however,
you can set the bound of the region with region(
)
. The simplest type of comparison is the matches(
)
method. It returns true
if the pattern
matches the complete region of the character sequence, and returns
false
otherwise. The lookingAt(
)
method is similar: it returns true if the pattern matches
the complete region, or if it matches some subsequence at the
beginning of the region. If the pattern does not match the start of
the region, lookingAt( ...
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.