简体   繁体   中英

What is the use of second limit in the quantifier {m,n} in the regular expression in python if it used in a non-greedy way?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM