XML technologies / Regular expressions / Java / Additional Pattern methods
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Besides its main compile factory methods, the Pattern class offers several helper methods:
split
Both forms of this method are discussed in detail starting under The one-argument split method.
String pattern()
This method returns the regular expression used to create the pattern as a string.
String toString()
This method does the same as
pattern(since Java 1.5).
int flags()
This method returns the numeric value of the flags used with
compile.
static String quote(String text)
This static method, available since Java 1.5, returns a string usable with
Pattern.compileas the regex argument so the text is interpreted literally, rather than as a regex.Pattern.quote("main()"), for example, returns the string ›\Qmain()\E‹, interpreted as the regex˹\Qmain()\E˼, which matches exactly the original string, ›main()‹.
static boolean matches(String muster, CharSequence suchtext)
This static function returns a Boolean indicating whether the regex in
musterexactly matches theCharSequence suchtext, or, as with thematchermethod, any object implementingCharSequence; see The matcher method. Essentially, this is equivalent to:
Pattern.compile(muster).matcher(suchtext).matches();To specify particular options or obtain more than a simple match-or-no-match result, use the more detailed methods discussed earlier.
If this method is called repeatedly, in a loop, it is much more efficient to compile the regex once into a
Patternobject and reuse that precompiled pattern for every search.
The one-argument split method
String[] split(CharSequence text)
This
Patternmethod accepts the argumenttext, of typeCharSequence, and returns an array of strings containing the pieces between matches of the object’s regex. The method is also available forStringobjects.
In the simple example
String[] resultat = Pattern.compile("\\.").split("209.204.146.22");, an array of four strings is returned: ›209‹, ›204‹, ›146‹ and ›22‹. They are separated in the subject text by matches of ˹\.˼. Here the delimiter is one literal character, but it can be an arbitrarily complex regex. For example, words can be roughly separated by using nonalphanumeric strings as delimiters:
String[] resultat = Pattern.compile("\\W+").split(Text);The string ›Was gibt's, Doc‹ is split into the four strings ›Was‹, ›gibt‹, ›s‹ and ›Doc‹, separated by the three matches. For non-ASCII text, consider replacing ˹\W+˼ with ˹\P{L}+˼ or ˹[^\p{L}\p{N}_]˼; see \p{...} and \P{...}.
Empty elements between consecutive matches
If the object’s regex matches at the beginning of the string Texts, the first element is an empty string, a valid string containing no characters. An empty string is likewise returned between consecutive matches. These empty strings represent the zero characters between matches. In the example
String[] resultat = Pattern.compile("\\s*,\\s*").split(", eins, zwei , ,, 3");, the delimiter is a comma with whitespace before and after it. This produces a five-element array: ›eins‹, ›zwei‹, two empty strings, and ›3‹.
Trailing empty elements, however, are suppressed:
String[] resultat = Pattern.compile(":").split(":xx:");This returns only two strings: an empty string and ›xx‹. If trailing empty elements matter, call the split method with two arguments.
The two-argument split method
String[] split(CharSequence text, int limit)
This version of
splitcontrols how often the regex is tried and how trailing empty elements are handled. It also exists under the same name in theStringclass.
The limit argument has different meanings depending on whether its value is below, equal to, or above zero.
split with a negative limit
Trailing empty array elements are not suppressed.
String[] resultat = Pattern.compile(":").split(":xx:", -1);therefore returns three strings: an empty string, ›xx‹, and another empty string.
split with a zero limit
An explicit second argument of zero is treated just like omitting it: trailing empty elements are suppressed.
split with a positive limit
If the limit argument is positive, it specifies the maximum number of returned elements. The regex is therefore applied at most limit-1 times. With a limit of 3, at most three strings are requested, separated by two matches.
After limit-1 matches, no further delimiters are sought; the entire remaining string becomes the last element.
If a string like the following
Friedl,Jeffrey,Eric Francis,America,Ohio,Rootstown
interests us only for its first three components, we can divide it into four parts using split, the three name components and a fourth containing everything else:
String[] NameInfo = Pattern.compile(",").split(Text, 4);
// NameInfo[0] ist der Nachname
// NameInfo[1] ist der Vorname
// NameInfo[2] enthält weitere Vornamen
// NameInfo[3] enthält alles andere, was wir im Weiteren sowieso nicht benötigen.Specifying a maximum saves work: why find every remaining field when only the first three matter? The limit argument can therefore improve efficiency.
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