简体   繁体   English

如果以非贪婪的方式使用python中的正则表达式中的量词{m,n},那么第二个限制的用途是什么?

[英]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. Python re.compile(r'\\w{3,5}?')的正则表达式将与具有至少三个非重叠字母数字和下划线字符的任何模式匹配。 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. 我的问题是'第二个限制在量词{3,5}的非贪婪使用中有任何用处,即即使五个被任何其​​他数字替换,结果也是相同的。 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? re.compile(r'\\w{3,5}?')=re.compile(r'\\w{3,6}?')=re.compile(r'\\w{3,7}?')=re.compile(r'\\w{3,}?')有人可以给我一个例子,其中第二个限制有用吗?

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 *? 一个123(\\w*?)总是在组1中没有产生任何值为*? matches zero or more chars, but as few as possible . 匹配零个或多个字符,但尽可能少

It means that \\w{3,5}? 这意味着\\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. 正则表达式将始终匹配3个字符,第二个参数将被“忽略”,因为它足以匹配单词char的3次出现。

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. 查看示例: Test: (\\w{3,5}?)-(\\d+)捕获组1中不同数量的字符,具体取决于字符串中字符匹配的匹配方式。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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