koliyat9811
koliyat9811

Reputation: 917

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}, 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

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

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.

enter image description here

Upvotes: 3

Related Questions