XML technologies / Regular expressions / Java / The Matcher object
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Once the regex is associated with the subject string, you can apply the regex in various ways and examine the results. For a Matcher object m, calling m.find() applies the regex to the subject string and returns a Boolean indicating success. On success, calling m.group() returns the matched text.
Before exploring individual Matcher methods, here is an overview of the data managed by the matcher. Each item links to details. The first list contains data the programmer can change; the second contains read-only data.
Data the programmer can set and change:
The
Patternobject supplied when creating the matcher. TheusePattern()method can specify a differentPatternobject (see Matcher usePattern(Pattern p)). The methodpattern()returns the currentPatternobject.The subject text, a string or any
CharSequence, supplied at construction can be replaced using thereset(text) method (see Matcher reset(CharSequence text)).The region in the subject text (see The matcher’s region). Normally, it spans the entire text, but the
regionmethod can restrict it to a subrange. Most matching functions, though not all, then search only that range.The methods
regionStartandregionEndreport the current region’s start and end offsets (see Setting and querying region boundaries). Theresetmethod (see Matcher reset()) resets the region to the entire text, as do methods that internally usereset(see Additional Matcher methods).The option anchoring bounds controls whether the region’s start and end count as the start and end of the string for line-boundary metacharacters (
\A ^ $ \z \Z) when the region does not cover the entire text.This option is enabled by default; query it with
hasAnchoringBoundsand change it withuseAnchoringBounds(see Anchoring bounds). Theresetmethod does not change this flag.The option transparent bounds controls whether look constructs—lookahead, lookbehind, and word boundaries—can look beyond a region that covers only part of the text.
This option is disabled by default; query it with
hasTransparentBoundsand set it with the methoduseTransparentBounds(see Transparent region boundaries). Theresetmethod does not change this flag.
The following matcher data can be queried but cannot be changed by a set method:
The number of capturing pairs in the current regex, available through the
groupCountmethod (see int groupCount()).The current position in the text, needed for the continue-searching behavior of
find(see boolean find()).The append position in the text: during Searching and replacing, the rest of the text is copied from this position into the result string (see Searching and replacing in detail).
A flag indicating whether the last search, successful or not, reached the text’s end. The
hitEndmethod returns its value (see boolean hitEnd()).The result of matching: after success, the match’s various values constitute the result (see Querying results). This includes the matched text, obtained with
group(), and its start and end indexes, obtained withstart()andend(), plus the corresponding data for each capturing pair, throughgroup(num),start(num)andend(num).The complete result can be saved using the
toMatchResultmethod as aMatchResultobject. AMatchResultobject has its owngroup,startandendmethods, working just like the matcher’s (see MatchResult toMatchResult()).A flag indicating whether a longer subject text could have caused failure; it is valid only after a successful search. It is set when a line-boundary metacharacter matches at the end of the match. Query it with
requireEnd(see boolean requireEnd()).
This condensed list is difficult to follow, but the items become clearer when methods are discussed by function in the following sections. The method table at the start of the chapter (see Alphabetical index of java.util.regex methods) helps navigation.
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
back to top