Advanced topics · Exercise 97 of 100
Basic regular expression matching
In this matcher, a literal matches itself, a dot matches any one letter, and a star after a literal or dot allows zero or more copies of that element. To match a whole string, try the possible repetition lengths and keep one that lets the rest of the pattern consume the remaining letters exactly.
Try it yourself
When the pattern a.*bc matches the whole string axxbc, how many letters does .* consume?
One-step hint
Align the fixed opening and ending letters of the pattern with the text. Bracket the span between them and count only the letters in it.
Worked solution
- The initial a matches the first letter. The final b and c must match the last two letters so that the pattern and string end together.
- This leaves xx for the repeated dot, so it consumes 2 letters.