|CIT597 Quiz 4 (Regex)
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.
One or more occurrences of the letter 'a'
The letters 'a' and 'b', then one or more 'c's
Zero or more whitespace characters
Any one digit
Any one character except 'a', 'e', 'i', 'o', or 'u'
Zero or more characters, excluding newline.
Either 'fish' or 'fowl'
The characters '</', then any one character except '>', then '>'
One letter or underscore, followed by one or more letters, digits, and/or
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.
- (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
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?
- What characters are matched by the zeroth capturing group?