Consider the following Python code:
>>> re.search(r'.*(99)', 'aa99bb').groups()
('99',)
>>> re.search(r'.*(99)?', 'aa99bb').groups()
(None,)
I don't understand why I don't catch 99 in the second example.
This is because the .* first matches the entire string. At that point, it's not possible to match 99 any more, and since the group is optional, the regex engine stops because it has found a successful match.
If on the other hand the group is mandatory, the regex engine has to backtrack into the .*.
Compare the following debug sessions from RegexBuddy (the part of the string matched by .* is highlighted in yellow, the part matched by (99) in blue):
.*(99):

.*(99)?:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With