Take this example:
import re
re.search(r"\bsr\.?\b","sr. manager")
<_sre.SRE_Match object; span=(0, 2), match='sr'>
This result is not what I had expected.
The ?
qualifier is greedy, so it should match as much text as possible (reference).
Reading the pattern it should say "match a word boundary, followed by "sr", followed by 0 or 1 dot (but as much characters as possible), followed by another word boundary". So I expected the patter to math "sr." and not just "sr". This is the workaround that I have found:
re.search(r"\bsr(\.|\b)","sr. manager")
<_sre.SRE_Match object; span=(0, 3), match='sr.'>
The non greedy version gives instead what I expected for the non-greedy version:
re.search(r"\bsr\.??\b","sr. manager")
<_sre.SRE_Match object; span=(0, 2), match='sr'>
Why is the greedy version not giving the answer I expect? What is wrong with my understanding of this type of qualifiers?