XML technologies / Regular expressions / Java / The Matcher object / Other Matcher methods
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
The following Matcher methods do not fit neatly into the preceding sections:
Matcher reset()
This method resets the matcher in almost every respect. In particular, it discards all previous match information, resets the text position to the beginning, and resets the region (see The matcher’s region) to the entire subject text. Only the anchoring-bounds and transparent-bounds flags (see Transparent region bounds and Anchoring bounds) are preserved.
The three methods
replaceAll,replaceFirstand the single-argument form offindinternally useresetand therefore reset the region to its default, the entire text.This method returns the matcher itself, allowing method chaining (see Method chaining).
Matcher reset(CharSequence text)
Like
reset(), this method resets the matcher, but also replaces the subject text with a new string or another object implementingCharSequence.When the same regex is applied to different strings, for example every line of a file, this method is more efficient than creating a new matcher each time.
This method returns the matcher itself, allowing method chaining (see Method chaining).
Pattern pattern()
The matcher’s
patternmethod returns its associatedPatternobject. The corresponding regex is obtained withm.pattern().pattern(), which calls thepatternmethod (see Other Pattern methods) of thePatternobject. Despite the identical name, that method does something quite different.
Matcher usePattern(Pattern p)
This method replaces the matcher’s associated
Patternobject with another. It does not callresetinternally, so it can start searching with a new regex from the current position. This technique is used in the example in Adding WIDTH and HEIGHT attributes to HTML IMG tags.usePatternhas existed only since Java 1.5.This method returns the matcher itself, allowing method chaining (see Method chaining).
String toString()
Also available since Java 1.5, this method produces a string containing basic matcher information, mainly useful for debugging. Its exact contents and formatting are unspecified and may change. In Java 1.6, the following fragment:
Matcher m = Pattern.compile("(\\w+)").matcher("ABC 123");
System.out.println(m.toString());
m.find();
System.out.println(m.toString());printed the following lines:
java.util.regex.Matcher[pattern=(\w+) region=0,7 lastmatch=]
java.util.regex.Matcher[pattern=(\w+) region=0,7 lastmatch=ABC]In Java 1.4.2, the
Matcherclass inherited fromjava.lang.ObjectthetoStringmethod, which returned only useless information such as ›java.util.regex.Matcher@480457‹.
Retrieving the matcher’s subject text
Matcher has no method returning the current subject text. The following fragment does this:
// Pattern wird nur einmal kompiliert und abgespeichert.
static final Pattern pPasstImmer = Pattern.compile("^");
// Gibt den mit dem Matcher verbundenen Suchtext zurück.
public static String text(Matcher m) {
// Region und aktuelles Pattern abspeichern.
Integer regionStart = m.regionStart();
Integer regionEnd = m.regionEnd();
Pattern pattern = m.pattern();
// Trick, um den Suchtext herauszuholen.
String text = m.usePattern(pPasstImmer).replaceFirst("");
// Matcher wieder in den unrsprünglichen Zustand bringen.
m.usePattern(pattern).region(regionStart, regionEnd);
// Suchtext zurückgeben.
return text;
}This uses replaceFirst with a regex matching the beginning of the string and replaces that match with the empty string: a no-op whose result is the unchanged subject text. This resets the matcher, so the original region and pattern are saved and restored after replacement. It is neither particularly elegant nor efficient, and always returns a string even when the original text had another CharSequence type. Still, until something better exists, it does the job.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln