This trail teaches the regular expression syntax supported by the
java.util.regex
API and presents several working examples to illustrate
how the various objects interact.
In the world of regular expressions, there are
many different flavors to choose from, such as
grep, Perl, Tcl, Python, PHP, and awk. The regular expression syntax in
the java.util.regex
API is most similar to that found in Perl.
java.util.regex
package primarily consists of three classes:
Pattern
,
Matcher
, and
PatternSyntaxException
.
Pattern
object is a compiled representation of a regular expression.
The Pattern
class provides no public constructors.
To create a pattern, you must first invoke one of its public static
compile
methods, which will then return a Pattern
object. These methods accept a regular expression as the first argument;
the first few lessons of this trail will teach you the required syntax.
Matcher
object is the engine that interprets the
pattern and performs match operations against an input string.
Like the Pattern
class, Matcher
defines no public constructors. You obtain a Matcher
object by invoking
the matcher
method on a Pattern
object.
PatternSyntaxException
object is an unchecked exception that indicates a syntax error in a regular expression pattern.