Reputation: 917
The regular expression in Python re.compile(r'\w{3,5}?')
will match with any pattern that have at least three non-overlapping alpha-numeric and underscore characters. My question here 'is the second limit has any use in this non greedy use of quantifier {3,5}, i.e. even if the five is replaced by any other number the result would be same. i.e. re.compile(r'\w{3,5}?')=re.compile(r'\w{3,6}?')=re.compile(r'\w{3,7}?')=re.compile(r'\w{3,}?')
Can some one give me an example where the second limit find any use?
Upvotes: 1
Views: 36
Reputation: 627292
When a lazily quantified pattern appears at the end of the pattern, it matches the minimum amount of chars it needs to match to return a value. A 123(\w*?)
will always yield no value inside Group 1 as *?
matches zero or more chars, but as few as possible.
It means that \w{3,5}?
regex will always match 3 word chars, and the second argument will be "ignored" as it is enough to match 3 occurrences of the word char.
If the lazy pattern is not at the end, the second argument is important.
See an example: Test: (\w{3,5}?)-(\d+)
captures different amount of chars in Group 1 depending on how match word chars there are in the strings.
Upvotes: 3