Regular Expressions
Regular expressions are used to specify string patterns. You can use regular expressions whenever you need to locate strings that match a particular pattern. For example, one of our sample programs locates all hyperlinks in an HTML file by looking for strings of the pattern <a href="...">. Of course, when specifying a pattern, the ... notation is not precise enough. You need to specify precisely what sequence of characters is a legal match. There is a special syntax that you need to use whenever you describe a pattern. Here is a simple example. The regular expression
[Jj]ava.+
matches any string of the following form:
- The first letter is a J or j.
- The next three letters are ava.
- The remainder of the string consists of one or more arbitrary characters.
For example, the string "javanese" matches the particular regular expression, but the string "Core Java" does not. As you can see, you need to know a bit of syntax to understand the meaning of a regular expression. Fortunately, for most purposes, a small number of straightforward constructs is sufficient.
- A character class is a set of character alternatives, enclosed in brackets, such as [Jj], [0-9], [A-Za-z], or [^0-9]. Here the - denotes a range (all characters whose Unicode value falls between the two bounds), and ^ denotes the complement (all characters except the ones specified).
- There are many predefined character classes such as \d (digits) or \p{Sc} (Unicode currency symbol). See Table 12-2 and 12-3.
- Most characters match themselves, such as the ava characters in the example above.
- The . symbol matches any character (except possibly line terminators, depending on flag settings).
- Use \ as an escape character, for example \. matches a period and \\ matches a backslash.
- ^ and $ match the beginning and end of a line respectively.
- If X and Y are regular expressions, then XY means "any match for X followed by a match for Y". X | Y means "any match for X or Y".
- You can apply quantifiers X+ (1 or more), X* (0 or more), and X? (0 or 1) to an expression X.
- By default, a quantifier matches the largest possible repetition that makes the overall match succeed. You can modify that behavior with suffixes ? (reluctant or stingy match-match the smallest repetition count) and + (possessive or greedy match-match the largest count even if that makes the overall match fail). For example, the string cab matches [a-z]*ab but not [a-z]*+ab. In the first case, the expression [a-z]* only matches the character c, so that the characters ab match the remainder of the pattern. But the greedy version [a-z]*+ matches the characters cab, leaving the remainder of the pattern unmatched.
- You can use groups to define subexpressions. Enclose the groups in ( ), for example ([+-]?)([0-9]+). You can then ask the pattern matcher to return the match of each group, or refer back to a group with \n, where n is the group number (starting with \1).
For example, here is a somewhat complex but potentially useful regular expression-it describes decimal or hexadecimal integers:
[+-]?[0-9]+|0[Xx][0-9A-Fa-f]+
Unfortunately, the expression syntax is not completely standardized between the various programs and libraries that use regular expressions. While there is consensus on the basic constructs, there are many maddening differences in the details. The Java regular expression classes use a syntax that is similar to, but not quite the same as, the one used in the Perl language. Table 12-4 shows all constructs of the Java syntax. For more information on the regular expression syntax, consult the API documentation for the Pattern class or the tutorial Mastering Regular Expressions by Jeffrey E. F. Friedl (Oracle and Associates, 1997). The simplest use for a regular expression is to test whether a particular string matches it. Here is how you program that test in Java. First construct a Pattern object from the string denoting the regular expression. Then get a Matcher object from the pattern, and call its matches method:
Pattern pattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher(input); if (matcher.matches()) . . .
Table 12-4. Regular expression syntax
Characters |
Explanation | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
c | The character c | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\unnnn, \xnn, \0n, \0nn, \0nnn | The character with the given hex or octal value | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\t, \n, \r, \f, \a, \e | The control characters tab, newline, return, form feed, alert, and escape | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\cc | The control character corresponding to the character c | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Character Classes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[C1C2. . .] | Any of the characters represented by C1, C2, . . . The Ci are characters, character ranges (c1-c2), or character classes | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[^. . .] | Complement of character class | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ . . . && . . .] | Intersection of two character classes | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Predefined Character Classes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
. | Any character except line terminators (or any character if the DOTALL flag is set) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\d | A digit [0-9] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\D | A non-digit [^0-9] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\s | A whitespace character [\t\n\r\f\x0B] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\S | A non-whitespace character | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\w | A word character [a-zA-Z0-9_] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\W | A non-word character | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\p{name} | A named character class-see Table 12-5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\P{name} | The complement of a named character class | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Boundary Matchers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
^ $ | Beginning, end of input (or beginning, end of line in multiline mode) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\b | A word boundary | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\B | A non-word boundary | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\A | Beginning of input | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\z | End of input | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\Z | End of input except final line terminator | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\G | End of previous match | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Quantifiers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
X? | Optional X | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
X* | X, 0 or more times | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
X+ | X, 1 or more times | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
X{n} X{n,} X{n,m} | X n times, at least n times, between n and m times | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Quantifier Suffixes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
? | Turn default (greedy) match into reluctant match | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
+ | Turn default (greedy) match into possessive match | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Set Operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
XY | Any string from X, followed by any string from Y | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
X|Y | Any string from X or Y | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Grouping | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(X) | Capture the string matching X as a group | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\n | The match of the nth group | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Escapes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\c | The character c (must not be an alphabetic character) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\Q . . . \E | Quote . . . verbatim | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(? . . . ) | Special construct-see API notes of Pattern class
Table 12-5. Predefined character class names
|