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}, ie even if the five is replaced by any other number the result would be same. ie 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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.