When to use the Pattern/Matcher paradigm
On the previous page, we saw that an alternative to calling String.matches()
is to use a more "explicit" approach, constructing a Pattern object and corresponding Matcher for each string to be matched. So why would you do this rather than just calling String.matches().
Performance
Before a match is performed against a regular expression, that expression must
be compiled. To compile the expression, the regex library
creates an internal object representation of that expression
that makes it efficient to perform matches. If you are performing several matches,
then it is more efficient to compile the expression once, and then create several
Matchers from the same compiled Pattern.
When you call matches() on a String, this is essentially just
a shortcut. Under the hood, the expression you pass in is compiled into a
Pattern object and then a Matcher is created from it, whose
matches() method is then called to get the result.
Setting options
There are several options, such as case sensitivity, that
can be set by passing a flags parameter into Pattern.compile().
Some of these parameters can actually be set in a "raw"
expression string, but the syntax of Pattern.compile() flags is
generally more convenient.
Finding multiple instances of a pattern within a string
As well as the simple matches() method, matchers actually provide a
find() method which can be used to find multiple instances of a given
pattern within a single string. For more details, see the
section on the Matcher.find method.
Capturing groups
An important feature of regular expressions is that the parts of the string
that match certain sub-expressions can be
captured or "pulled out" of the matcher when a match is
found. On the next page, we will look in more detail at capturing groups.
If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.
Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.