15.5. Regular Expressions

You saw some elementary capability for searching strings when I discussed the String class back in Chapter 4. You have much more sophisticated facilities for analyzing strings by searching for patterns known as regular expressions. Regular expressions are not unique to Java. Perl is perhaps better known for its support of regular expressions. Many word processors, especially on Unix, support regular expressions, and there are specific utilities for regular expressions, too.

So what is a regular expression? A regular expression is simply a string that describes a pattern that is to be used to search for matches within some other string. It's not simply a passive sequence of characters to be matched, though. A regular expression is essentially a mini-program for a specialized kind of computer called a state-machine. This isn't a real machine but a piece of software specifically designed to interpret a regular expression and analyze a given string based on the operations implicit in a regular expression.

The regular expression capability in Java is implemented through two classes in the java.util.regex package: the Pattern class, which defines objects that encapsulate regular expressions, and the Matcher class, which defines an object that encapsulates a state-machine that can search a particular string using a given Pattern object. The java.util.regex package also defines the PatternSyntaxException class, which defines exception objects thrown when a syntax ...

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.