Java Reference
In-Depth Information
StringBuffer
, and
StringBuilder
classes so you can pass a reference of any of these types to the meth-
od. The
java.nio.CharBuffer
class also implements
CharSequence
, so you can pass the contents of a
CharBuffer
to the method, too. This means that if you use a
CharBuffer
to hold character data you have
read from a file, you can pass the data directly to the
matcher()
method to be searched.
An advantage of Java's implementation of regular expressions is that you can reuse a
Pattern
object to
create
Matcher
objects to search for the pattern in a variety of strings. To use the same pattern to search an-
other string, you just call the
matcher()
method for the
Pattern
object with the new string as the argument.
You then have a new
Matcher
object that you can use to search the new string.
You can also change the string that a
Matcher
object is to search by calling its
reset()
method with a
new string as the argument. For example:
matchHad.reset("Had I known, I would not have eaten the haddock.");
This replaces the previous string,
sentence
, in the
Matcher
object, so it is now capable of searching
the new string. Like the
matcher()
method in the
Pattern
class, the parameter type for the
reset()
method is
CharSequence
, so you can pass a reference of type
String
,
StringBuffer
,
StringBuilder
, or
CharBuffer
to it.
Searching a String
Now that you have a
Matcher
object, you can use it to search the string. Calling the
find()
method for the
Matcher
object searches the string for the next occurrence of the pattern. If it finds the pattern, the method
stores information about where it was found in the
Matcher
object and returns
true
. If it doesn't find the
pattern, it returns
false
. When the pattern has been found, calling the
start()
method for the
Matcher
object returns the index position in the string where the first character in the pattern was found. Calling the
end()
method returns the index position following the last character in the pattern. Both index values are
returned as type
int
. Therefore, you could search for the first occurrence of the pattern like this:
if(m.find()) {
System.out.println("Pattern found. Start: " + m.start() + " End: " + m.end());
} else {
System.out.println("Pattern not found.");
}
Note that you must not call
start()
or
end()
for the
Matcher
object before you have succeeded in find-
ing the pattern. Until a pattern has been matched, the
Matcher
object is in an undefined state and calling
either of these methods results in an exception of type
IllegalStateException
being thrown.
You usually want to find all occurrences of a pattern in a string. When you call the
find()
method,
searching starts either at the beginning of this matcher's region, or at the first character not searched by a
previous call to
find()
. Thus, you can easily find all occurrences of the pattern by searching in a loop like
this:
while(m.find()) {
System.out.println(" Start: " + m.start() + " End: " + m.end());
}