Java Reference
In-Depth Information
13.3. Regular Expression Matching
The package
java.util.regex
provides you a way to find if a string
matches a general description of a category of strings called a
regular
expression.
A regular expression describes a class of strings by using
wildcards that match or exclude groups of characters, markers to require
matches in particular places, etc. The package uses a common kind of
regular expression, quite similar to those used in the popular
perl
pro-
gramming language, which itself evolved from those used in several Unix
utilities.
You can use regular expressions to ask if strings match a pattern and
pick out parts of strings using a rich expression language. First you will
learn what regular expressions are. Then you will learn how to compile
and use them.
13.3.1. Regular Expressions
A full description of regular expressions is complex and many other works
describe them. So we will not attempt a complete tutorial, but instead
will simply give some examples of the most commonly used features. (A
full reference alone would take several pages.) A list of resources for un-
derstanding regular expressions is in "
Further Reading
" on page
758
.
Regular expressions search in character sequences, as defined by
java.lang.CharSequence
, implemented by
String
and
StringBuilder
. You can
implement it yourself if you want to provide new sources.
A regular expression defines a pattern that can be applied to a character
sequence to search for matches. The simplest form is something that is
matched exactly; the pattern
xyz
matches the string
xyzzy
but not the
string
plugh
. Wildcards make the pattern more general. For example,
.
(dot) matches any single character, so the pattern
.op
matches both
hop
and
pop
, and
*
matches zero or more of the thing before it, so
xyz*
matches
xy
,
xyz
, and
xyzzy
.