Trail: Essential Classes
Lesson: Regular Expressions
Summary
Summary
In this chapter, you learned that regular expressions are a way to describe a set of strings based on common characteristics shared by each string in the set. The Java programming language supports regular expressions via the java.util.regex package, primarily through the Pattern, Matcher, and PatternSyntaxException classes.

The most basic form of pattern matching supported by this API is the match of a string literal. You can also specify metacharacters — characters that carry special meaning — that will be interpreted by the matcher.

A character class is a set of characters enclosed within square brackets. It specifies the characters that will successfully match a single character from a given input string. You can define your own character classes, or use the predefined character classes included in the API.

Quantifiers allow you to specify the number of occurrences to match against. There are three different kinds of quantifiers: greedy, reluctant, and possessive.

Capturing groups provide a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses, and are numbered by counting their opening parentheses from left to right. The section of the input string matching the capturing group(s) is saved in memory for later recall via a backreference. A backreference is specified in the regular expression as a backslash "\" followed by a digit indicating the number of the group to be recalled.

Boundary matchers make your matches more precise by specifying a match location within the input string. The regex API provides boundary matchers for the following locations: the beginning of a line, the end of a line, word boundaries, non-word boundaries, the beginning of the input, the end of the input, and the end of the previous match.

Finally, you explored the Pattern, Matcher, PatternSyntaxException classes in detail to learn about their additional functionality, including their method equivalents in java.lang.String.

Previous page: Additional Resources
Next page: Questions and Exercises: Regular Expressions