Questions and Exercises: Regular Expressions


  • What are the three public classes in the java.util.regex package? Describe the purpose of each.

  • Consider the string literal "foo" . What is the start index? What is the end index? Explain what these numbers mean.

  • What is the difference between an ordinary character and a metacharacter? Give an example of each.

  • How do you force a metacharacter to act like an ordinary character?

  • What do you call a set of characters enclosed in square brackets? What is it for?

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

  • For each of \d , \s , and \w , write two simple expressions that match the opposite set of characters.

  • Consider the regular expression (dog){3} . Identify the two subexpressions. What string does the expression match?


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