CIT597 Quiz 4 (Regex)
Fall, 2007
Name_________________________________________

Please keep all answers short and to the point. Read each question and answer it; don't add information that wasn't asked for.

  1. (50 points) Tell, in words, what each of the following patterns will match.

    1. a+
      One or more occurrences of the letter 'a'

    2. abc+
      The letters 'a' and 'b', then one or more 'c's

    3. \s*
      Zero or more whitespace characters

    4. [0-9]
      Any one digit

    5. [^aeiou]
      Any one character except 'a', 'e', 'i', 'o', or 'u'

    6. .*
      Zero or more characters, excluding newline.

    7. (fish|fowl)
      Either 'fish' or 'fowl'

    8. </[^>]>
      The characters '</', then any one character except '>', then '>'

    9. [a-z_A-Z][a-z_A-Z0-9]+
      One letter or underscore, followed by one or more letters, digits, and/or underscores


    10. (\d*\.\d+)|(\d+\.\d*)
      A decimal point with digits on at least one side.
      Zero or more digits, a period, and one or more digits; or, one or more digits, a period, and zero or more digits.







  2. (15 points) There are three ways you can use a regular expression to try to match a string. What are those three ways? (Short answers, please.)

    Match the entire string.
    Match the beginning of the string.
    Match some substring of the string.




  3. (5 points) Write the regular expression \d+\.\d+ as a Java quoted String.
    "\\d+\\.\\d+"


  4. (15 points) Write the Java statements (not a complete program!) to set a boolean variable b to true if and only if the string s contains a digit.

    Pattern p = Pattern.compile("\\d");
    Matcher m = p.matcher(s);
    b = m.find();



  5. (5 points) By default, quantifiers are greedy. What does this mean?

    They match the longest substring they can that still allows the rest of the pattern to succeed.


  6. (10 points) If you apply the regular expression ([a-z]*)([0-9]+)([a-z]*) to the string abc123xyz

    1. What characters are matched by the second capturing group?
      123

    2. What characters are matched by the zeroth capturing group?
      abc123xyz