Back to Student Worksheet
CS Grade 9-12 Answer Key

CS: Regular Expressions: Patterns and Matching

Finding text patterns with regex syntax

Answer Key
Name:
Date:
Score: / 12

CS: Regular Expressions: Patterns and Matching

Finding text patterns with regex syntax

CS - Grade 9-12

Instructions: Read each problem carefully. For matching questions, explain why the pattern matches or does not match. Use standard regular expression notation.
  1. 1

    The regular expression cat is tested against these strings: cat, catalog, scatter, dog. Which strings contain a match, and why?

    A regex can match part of a string unless anchors are used.

    The strings cat, catalog, and scatter contain a match because the letters c, a, and t appear next to each other in that order. The string dog does not contain a match.
  2. 2

    Explain the difference between the patterns cat and ^cat$ when matching strings.

    The pattern cat matches the letters c, a, and t anywhere inside a string. The pattern ^cat$ matches only the entire string cat because ^ marks the start of the string and $ marks the end of the string.
  3. 3

    Write a regular expression that matches either color or colour.

    Use the optional quantifier for the letter that may or may not appear.

    A correct regular expression is colou?r. The question mark makes the letter u optional, so the pattern matches color and colour.
  4. 4

    The pattern gr[ae]y is tested against gray, grey, groy, and grAy. Which strings match?

    The strings gray and grey match because [ae] means the third character can be either a or e. The strings groy and grAy do not match in a case-sensitive match.
  5. 5

    Write a regular expression that matches a three-digit number such as 042, 107, or 999. It should not match a two-digit or four-digit number if the whole string is being checked.

    Use a digit shorthand, a quantifier, and start and end anchors.

    A correct regular expression is ^\d{3}$. The \d matches a digit, {3} requires exactly three digits, and the anchors make sure the whole string is exactly three digits long.
  6. 6

    A username must start with a letter and then contain 3 to 11 more characters. The remaining characters may be letters, digits, or underscores. Write a regular expression for the whole username.

    Use one character class for the first character and another repeated character class for the rest.

    A correct regular expression is ^[A-Za-z][A-Za-z0-9_]{3,11}$. The first character must be a letter, and the next 3 to 11 characters may be letters, digits, or underscores.
  7. 7

    For the pattern a+b*, decide whether each string matches the entire pattern: a, ab, aaabb, b, and aaac. Explain your choices.

    The strings a, ab, and aaabb match because a+ means one or more a characters and b* means zero or more b characters. The string b does not match because it has no a. The string aaac does not match because c is not allowed.
  8. 8

    What is matched by the regular expression \bcat\b in the sentence The cat sat in the category box?

    A word boundary occurs between a word character and a non-word character, or at the start or end of a string.

    The pattern matches the word cat in The cat sat in the category box. It does not match the cat part of category because \b requires word boundaries around cat.
  9. 9

    Write a regular expression that matches a simple school email address ending in @school.edu. The part before @ must contain one or more lowercase letters, digits, dots, underscores, or hyphens.

    Remember that a dot has a special meaning unless it is escaped.

    A correct regular expression is ^[a-z0-9._-]+@school\.edu$. The character class allows the username characters, + requires at least one of them, and the escaped dot matches a literal period in school.edu.
  10. 10

    The pattern ^[A-Z]{2}\d{4}$ is used to check ID codes. Which of these match: AB1234, A12345, ab1234, XY9876, and ZQ12B4?

    The strings AB1234 and XY9876 match because they have exactly two uppercase letters followed by exactly four digits. A12345 does not match because it has only one starting letter, ab1234 does not match because the letters are lowercase, and ZQ12B4 does not match because B appears where a digit is required.
  11. 11

    Explain what the dot means in the pattern h.t. Then give two strings that match and one string that does not match.

    The dot stands for exactly one character, not zero characters or many characters.

    In the pattern h.t, the dot matches any single character except usually a newline. Examples that match include hat and hot. The string heat does not match the whole pattern because it has two characters between h and t.
  12. 12

    A log file has lines like ERROR: disk full, WARNING: high memory, and INFO: started. Write a regular expression that matches only lines beginning with ERROR: or WARNING:.

    A correct regular expression is ^(ERROR|WARNING):. The ^ anchor requires the match to start at the beginning of the line, and the group with | matches either ERROR or WARNING before the colon.
LivePhysics™.com CS - Grade 9-12 - Answer Key