XML technologies / Regular expressions / Java / The factory method Pattern.compile
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
A Pattern object is created with Pattern.compile. The first argument is a string interpreted as a regex (see Strings in Java). The second argument accepts the compilation options listed in the table Regex modes in java.util.regex. The following example creates, from the regex in the string beispielRegex a Pattern object intended for case-insensitive matching.
Pattern pat = Pattern.compile(beispielRegex,
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);The symbolic constants from Pattern for compilation options are somewhat unwieldy and cumbersome. (Note: especially in examples in a book with limited line width—I speak from experience!) I therefore usually use inline mode modifiers in the regex (see Case-insensitive mode). Examples of ˹(?x)˼ appear in Code example: match results and several of ˹(?i)˼ in Adding WIDTH and HEIGHT attributes to HTML IMG tags.
On the other hand, these “cumbersome” spelled-out symbols are precisely what help beginners understand. Without the line-width limitation, for the second argument of Pattern.compile in The matcher’s region I would much rather write
Pattern.UNIX_LINES | Pattern.CASE_INSENSITIVErather than inserting a potentially incomprehensible ˹(?id)˼ at the beginning of the regex.
As the method’s name suggests, this step parses the textual regular expression and translates it into an internal form. The art of writing regular expressions describes this step in detail (see How a regular expression is applied). Here it is enough to know that this translation is often the most time-consuming part of applying a regex to a string. That is precisely why compilation and matching are separate steps: a regex can be compiled once and reused.
If a regex is used only once, this does not matter. But when the same regex is applied many times, for example to every line of a file, compiling it only once can be very worthwhile.
Calling Pattern.compile(...) can throw two types of exception: a syntactically invalid regex produces PatternSyntaxException, and invalid option values produce IllegalArgumentException.
The matcher method
The Pattern object also contains some convenience functions, discussed later, but generally all the work is done by a single method: matcher. It takes one argument: the string to search. (Note: the argument to matcher can be any object implementing the CharSequence interface, for example String, StringBuffer, CharBuffer and others.) This does not yet apply the regex; it only associates the Pattern object with the subject string in a particular way. The matcher method returns a Matcher object.
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