'Python regex: Match 1 or 2 digits, return longest

I'm struggling to understand my error. Consider these examples:

A 9 minutes delay experienced

a 10 minutes delay

I want to extract 9 and 10, respectively.

So I tried this:

.*(\d{1,2})(?:\s)?(min|m|hour|hr|h|minutes|minute) 

test link

But for the last case, group returns 0 instead of 10. I thought \d{1,2} was greedy, and hence would return the longest match. Other, unsuccessful attempts:

.*(\d+)(?:\s)?(min|m|hour|hr|h|minutes|minute) 
.*([0-9]+)(?:\s)?(min|m|hour|hr|h|minutes|minute) 


Solution 1:[1]

Hmm .* is greedy allready, however you want to extract two numbers - keep it simple, something like .*?(\d+).*(\d+) will do the job

Solution 2:[2]

".*?([0-9]+).*" is an answer.

.* is greedy : it will match the more possible. "A 1" in the "A 10 minutes delay" case. Letting the ([0-9]+) group with only "0" left.

.*? is lazy : it will match the minimum possible, letting the priority to the subsequent ([0-9]+) group to match the 2 digits "10". ( + is greedy, it wants to match the more possible).

To test the behaviour of the "?": https://regex101.com/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lars Grundei
Solution 2