Download the JDK
Search the Tutorials
Trail: Essential Classes
Lesson: Regular Expressions
Home Page > Essential Classes > Regular Expressions
« Previous • TOC
Answers to Questions and Exercises:

### Questions

> 1. Question: What are the three public classes in the `java.util.regex` package? Describe the purpose of each.

>    Answer:

>    - `Pattern` instances are compiled representations of regular expressions.
>    - `Matcher` instances are engines that interpret patterns and perform match operations against input strings.
>    - `PatternSyntaxException` defines an unchecked exception indicating a syntax error in a regular expression.

> 2. Question: Consider the string literal `"foo"`. What is the start index? What is the end index? Explain what these numbers mean.

>    Answer: Each character in the string resides in its own cell. Index positions point between cells. The string `"foo"` starts at index 0 and ends at index 3, even though the characters only occupy cells 0, 1, and 2.

> 3. Question: What is the difference between an ordinary character and a metacharacter? Give an example of each.

>    Answer: An ordinary character in a regular expression matches itself. A metacharacter is a special character that affects the way a pattern is matched. The letter `A` is an ordinary character. The puncuation mark `.` is a metacharacter that matches any single character.

> 4. Question: How do you force a metacharacter to act like an ordinary character?

>    Answer: There are two ways:

>    - Precede the metacharacter with a backslash (`\`);
>    - Enclose the metacharacter within the quote expressions, `\Q` (at the beginning) and `\E` (at the end).

> 5. Question: What do you call a set of characters enclosed in square brackets? What is it for?

>    Answer: This is a character class. It matches any single character that is in the class of characters specified by the expression between the brackets.

> 6. Question: Here are three predefined character classes: `\d`, `\s`, and `\w`. Describe each one, and rewrite it using square brackets.

>    Answer:

>    `\d` | Matches any digit. | `[0-9]`
>    `\s` | Matches any white space character. | `[ \t\n-x0B\f\r]`
>    `\w` | Matches any word character. | `[a-zA-Z_0-9]`

> 7. Question: For each of `\d`, `\s`, and `\w`, write two simple expressions that match the opposite set of characters.

>    Answer:

>    `\d` | `\D` | `[^\d]`
>    `\s` | `\S` | `[^\s]`
>    `\w` | `\W` | `[^\w]`

> 8. Question: Consider the regular expression `(dog){3}`. Identify the two subexpressions. What string does the expression match?

>    Answer: The expression consists of a capturing group, `(dog)`, followed by a greedy quantifier `{3}`. It matches the string "dogdogdog".

### Exercises

> 1. Exercise: Use a backreference to write an expression that will match a person's name only if that person's first name and last name are the same.

>    Solution: `([A-Z][a-zA-Z]*)\s\1`

Previous page: Questions and Exercises: Regular Expressions