Java Reference
In-Depth Information
Method
replaceFirst
(line 33) replaces the first occurrence of a pattern match. Java
String
s are immutable; therefore, method
replaceFirst
returns a new
String
in which
the appropriate characters have been replaced. This line takes the original
String
and
replaces it with the
String
returned by
replaceFirst
. By iterating three times we replace
the first three instances of a digit (
\d
) in
secondString
with the text
"digit"
.
Method
split
divides a
String
into several substrings. The original is broken in any
location that matches a specified regular expression. Method
split
returns an array of
String
s containing the substrings between matches for the regular expression. In line 39,
we use method
split
to tokenize a
String
of comma-separated integers. The argument is
the regular expression that locates the delimiter. In this case, we use the regular expression
",\\s*"
to separate the substrings wherever a comma occurs. By matching any white-
space characters, we eliminate extra spaces from the resulting substrings. The commas and
white-space characters are not returned as part of the substrings. Again, the Java
String
",\\s*"
represents the regular expression
,\s*
. Line 40 uses
Arrays
method
toString
to
display the contents of array
results
in square brackets and separated by commas.
Classes
Pattern
and
Matcher
In addition to the regular-expression capabilities of class
String
, Java provides other class-
es in package
java.util.regex
that help developers manipulate regular expressions. Class
Pattern
represents a regular expression. Class
Matcher
contains both a regular-expression
pattern and a
CharSequence
in which to search for the pattern.
CharSequence
(package
java.lang
) is an
interface
that allows read access to a
sequence of characters. The interface requires that the methods
charAt
,
length
,
subSe-
quence
and
toString
be declared. Both
String
and
StringBuilder
implement interface
CharSequence
, so an instance of either of these classes can be used with class
Matcher
.
Common Programming Error 14.2
A regular expression can be tested against an object of any class that implements interface
CharSequence
, but the regular expression must be a
String
. Attempting to create a reg-
ular expression as a
StringBuilder
is an error.
If a regular expression will be used only once,
static
Pattern
method
matches
can
be used. This method takes a
String
that specifies the regular expression and a
CharSe-
quence
on which to perform the match. This method returns a
boolean
indicating
whether the search object (the second argument)
matches
the regular expression.
If a regular expression will be used more than once (in a loop, for example), it's more
efficient to use
static
Pattern
method
compile
to create a specific
Pattern
object for
that regular expression. This method receives a
String
representing the pattern and
returns a new
Pattern
object, which can then be used to call method
matcher
. This
method receives a
CharSequence
to search and returns a
Matcher
object.
Matcher
provides method
matches
, which performs the same task as
Pattern
method
matches
, but receives no arguments—the search pattern and search object are encapsu-
lated in the
Matcher
object. Class
Matcher
provides other methods, including
find
,
lookingAt
,
replaceFirst
and
replaceAll
.
Figure 14.24 presents a simple example that employs regular expressions. This pro-
gram matches birthdays against a regular expression. The expression matches only birth-
days that do not occur in April and that belong to people whose names begin with
"J"
.