Search⌘ K
AI Features

Puzzle 26: Explanation

Explore how greedy matching in Python regular expressions impacts output results. Learn to adjust regex patterns by using nongreedy quantifiers, helping you solve puzzles that involve complex string matching. This lesson helps you analyze regex behavior and improve your problem-solving skills with real code examples.

We'll cover the following...

Try it yourself

Try running the code below to verify the result:

Python 3.8
import re
text = 'The vote was 65 in favour, 43 against and 21 abstentions'
match = re.search(r'(\d+).*(\d+).*(\d+)', text)
print(match.group(1), match.group(2), match.group(3))

Explanation

Most people expect the answer to be 65 43 21. The reason for the actual output is that the .* ...