Which regular expression matches cat and bat, but not at?
2025
Which regular expression matches cat and bat, but not at?
- A.
^.[cb]at$
- B.
[cat|bat]
- C.
^at$
- D.
^[cb]at$
- E.
.*at$
Attempted by 21 students.
Show answer & explanation
Correct answer: D
Correct answer: ^[cb]at$
The anchors ^ and $ make the pattern match the complete string.
The character class [cb] allows either c or b, followed by at. This gives cat and bat.
Because the first character is required, plain at does not match.