Java Reference
In-Depth Information
These same time gains can be had by Java developers. Regular expression support has been
in the standard Java runtime for ages and is well integrated (e.g., there are regex methods in
the standard class java.lang.String and in the “new I/O” package). There are a few other
regex packages for Java, and you may occasionally encounter code using them, but pretty
well all code from this century can be expected to use the built-in package. The syntax of
Java regexes themselves is discussed in Regular Expression Syntax , and the syntax of the
Java API for using regexes is described in Using regexes in Java: Test for a Pattern . The re-
maining recipes show some applications of regex technology in Java.
See Also
Mastering Regular Expressions by Jeffrey Friedl (O'Reilly) is the definitive guide to all the
details of regular expressions. Most introductory topics on Unix and Perl include some dis-
cussion of regexes; Unix Power Tools devotes a chapter to them.
Regular Expression Syntax
Problem
You need to learn the syntax of Java regular expressions.
Solution
Consult Table 4-1 for a list of the regular expression characters.
Discussion
These pattern characters let you specify regexes of considerable power. In building patterns,
you can use any combination of ordinary text and the metacharacters , or special characters,
in Table 4-1 . These can all be used in any combination that makes sense. For example, a+
means any number of occurrences of the letter a , from one up to a million or a gazillion. The
pattern Mrs? \. matches Mr. or Mrs. And .* means “any character, any number of times,” and
is similar in meaning to most command-line interpreters' meaning of the \* alone. The pat-
tern \d+ means any number of numeric digits. \d{2,3} means a two- or three-digit number.
Search WWH ::




Custom Search