Datasheet
Reluctant (Lazy) Operator Description
X?? Matches X zero or one time
X*? Matches X zero or more times
X+? Matches X one or more times
X{n}? Matches X exactly n times, where n is any number
X{n,}? Matches X at least n times
X{n,m}? Matches X at least n, but no more than m times
The language also supports capturing groups of matching characters by using parentheses inside the
regular expression. A back reference can be used to reference one of these matching subgroups. A back
reference is denoted by a backslash followed by a number corresponding to the number of a subgroup.
In the string
(A(B)), the zero group is the entire expression, then subgroups start numbering after each
left parenthesis. Therefore,
A(B) is the first subgroup, and B is the second subgroup. The back references
then allow a string to be matched. For example, if you want to match the same word appearing twice in
a row, you might use
[([a-zA-Z])\b\1]. Remember that the \b stands for a word boundary. Because
the character class for letters is inside parentheses, the text that matched can then be referenced using the
back reference meta-character
\1.
The Pattern Class
The Pattern class is responsible for compiling and storing a specified regular expression. There are flags
that control how the regular expression is treated. The
regex is compiled to provide for efficiency. The tex-
tual representation of a regular expression is meant for ease of use and understanding by programmers.
Method Description
static Pattern compile(String regex) The compile method accepts a regular expression
static Pattern compile(String regex,
in a string and compiles it for internal use. The
int flags)
variant form allows you to specify flags that mod-
ify how the regular expression is treated.
static boolean matches(String regex, Compiles a specified regular expression and
CharSequence input) matches it against the input. Returns true if the
regular expression describes the input data, and
false otherwise. Use this only for quick matches. To
match a regular expression repeatedly against dif-
ferent input, the regular expression should be com-
piled only once.
static String quote(String s) Returns a literal regular expression that will match
the string passed in. The returned string starts with
\Q followed by the string passed in, and ends with
\E. These are used to quote a string, so what would
be meta-characters in the regular expression lan-
guage are treated literally.
Table continued on following page
65
Chapter 1: Key Java Language Features and Libraries
05_777106 ch01.qxp 11/28/06 10:43 PM Page 65