Which of the following strings would match the regular expression : p+ [3 –…
2017
Which of the following strings would match the regular expression : p+ [3 – 5]∗ [xyz] ?
I. p443y II. p6y
III. 3xyz IV. p35z
V. p353535x VI. ppp5
- A.
I, III and VI only
- B.
IV, V and VI only
- C.
II, IV and V only
- D.
I, IV and V only
Attempted by 44 students.
Show answer & explanation
Correct answer: D
Interpretation: read the pattern as p+ [3-5]* [xyz]? where
p+ means one or more occurrences of the character 'p'.
[3-5]* means zero or more characters, each of which is '3', '4' or '5'.
[xyz]? means an optional single character that is either 'x', 'y' or 'z'.
Now test each candidate string:
I. p443y — starts with 'p'; '443' are all in {3,4,5}; ends with 'y' which is allowed by the optional [xyz], so it matches.
II. p6y — '6' is not in [3-5], so it does not match.
III. 3xyz — does not start with 'p', so it does not match.
IV. p35z — starts with 'p'; '3' and '5' are in [3-5]; ends with 'z' in [xyz], so it matches.
V. p353535x — starts with 'p'; the sequence 353535 is composed only of 3s and 5s; ends with 'x' in [xyz], so it matches.
VI. ppp5 — 'ppp' satisfies p+; '5' is allowed by [3-5]*; the optional [xyz] can be absent, so it matches.
Conclusion: The strings that match are I, IV, V and VI.
Note: None of the provided option choices exactly list I, IV, V and VI, so the original indicated correct option is incorrect. The correct matching set is I, IV, V and VI.