Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Using patterns to search for sequences of characters (i.e., strings) in the input is a powerful technique that can be used to search, modify, and maintain text-based data (e.g., XML data, log files, comma-separated values (CSV)). The java.util.regex package in the Java Standard Library provides support for string pattern matching that is based on regular expressions. Such an expression is specified using a special notation, which is precisely defined. A regular expression thus defines a pattern that represents a set of strings that we are interested in matching against characters in the input. We will use the term regular expression and pattern synonymously.
Before we can do string pattern matching with a regular expression, we have to compile it, i.e., turn it into a representation that can be used with an engine (also called an automaton) that can read the characters in the input and match them against the pattern. As we shall see, the java.util.Pattern class allows us to compile a regular expression, and the java.util.Matcher class allows us to create an engine for string pattern matching with the compiled regular expression.