The Java™ Tutorials
Hide TOC
Questions and Exercises
Trail: Essential Classes
Lesson: Regular Expressions

Questions and Exercises: Regular Expressions


  1. What are the three public classes in the java.util.regex package? Describe the purpose of each.
  2. Consider the string literal "foo". What is the start index? What is the end index? Explain what these numbers mean.
  3. What is the difference between an ordinary character and a metacharacter? Give an example of each.
  4. How do you force a metacharacter to act like an ordinary character?
  5. What do you call a set of characters enclosed in square brackets? What is it for?
  6. Here are three predefined character classes: \d, \s, and \w. Describe each one, and rewrite it using square brackets.
  7. For each of \d, \s, and \w, write two simple expressions that match the opposite set of characters.
  8. Consider the regular expression (dog){3}. Identify the two subexpressions. What string does the expression match?


  1. 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.

Check your answers.

Previous page: Additional Resources
Next page: End of Trail