Name
Pattern
Synopsis
This
class represents a regular expression. It
has no public constructor: obtain a Pattern
by
calling one of the static compile( )
methods,
passing the string representation of the regular expression, and an
optional bitmask of flags that modify the behavior of the regex.
pattern( )
and flags( )
return
the string form of the regular expression and the bitmask that were
passed to compile( )
.
If you want to perform only a single match operation with a regular
expression, and don’t need to use any of the flags,
you don’t have to create a
Pattern
object: simply pass the string
representation of the pattern and the CharSequence
to be matched to the static matches( )
method: the
method returns true
if the specified pattern
matches the complete specified text, or returns
false
otherwise.
Pattern
represents a regular expression, but does
not actually define any primitive methods for matching regular
expressions to text. To do that, you must create a
Matcher
object that encapsulates a pattern and the
text it is to be compared with. Do this by calling the
matcher( )
method and specifying the
CharSequence
you want to match against. See
Matcher
for a description of what you can do with
it.
The split( )
methods are the exception to the rule
that you must obtain a Matcher
in order to be able
to do anything with a Pattern
(although they
create and use a Matcher
internally). They take a
CharSequence
as input, and split it into substrings, using text that matches the regular expression ...
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.