Please keep all answers short and to the point. Read each question and answer
it; don't add information that wasn't asked for.
(50 points) Tell, in words, what each of the following patterns will match.
a+ One or more occurrences of the letter 'a'
abc+ The letters 'a' and 'b', then one or more 'c's
\s* Zero or more whitespace characters
[0-9] Any one digit
[^aeiou] Any one character except 'a', 'e', 'i', 'o', or 'u'
.* Zero or more characters, excluding newline.
(fish|fowl) Either 'fish' or 'fowl'
</[^>]> The characters '</', then any one character except '>', then '>'
[a-z_A-Z][a-z_A-Z0-9]+ One letter or underscore, followed by one or more letters, digits, and/or
underscores
(\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.
(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.
(5 points) Write the regular expression \d+\.\d+ as a Java quoted String. "\\d+\\.\\d+"
(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 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.
(10 points) If you apply the regular expression ([a-z]*)([0-9]+)([a-z]*)
to the string abc123xyz
What characters are matched by the second capturing group? 123
What characters are matched by the zeroth capturing group? abc123xyz