.* 和 .* 有什么区别?在正则表达式中?
What is the difference between .* and .*? in a regular expression?
我正在尝试了解常规表达式。在调查 re.match
和 re.search
之间的区别时,我看到一个(有争议的)声明 re.match('(.*?)word(.*?)',string)
比 re.search("word",string)
快 我没有看到 .*?
和 .*
之间的区别,也没有看到需要尾随 (.*?)
.
要理解任何正则表达式,首先应该是 https://regex101.com/。在这种情况下,这就是它所说的两者之间的唯一区别:
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
*? matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
从那里,您可以输入示例文本以实时测试表达式并查看实际差异。
参见the documentation。 ?
使得 *
non-greedy,即,它将尝试匹配尽可能少的重复,而不是尽可能多的重复。
在您的示例 re.match('(.*?)word(.*?)',string)
中,这意味着前导 .
越少越好,因此请尝试找到最早的 word
而不是最后一个。尾随 (.*?)
确实毫无意义。
我正在尝试了解常规表达式。在调查 re.match
和 re.search
之间的区别时,我看到一个(有争议的)声明 re.match('(.*?)word(.*?)',string)
比 re.search("word",string)
快 我没有看到 .*?
和 .*
之间的区别,也没有看到需要尾随 (.*?)
.
要理解任何正则表达式,首先应该是 https://regex101.com/。在这种情况下,这就是它所说的两者之间的唯一区别:
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
*? matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
从那里,您可以输入示例文本以实时测试表达式并查看实际差异。
参见the documentation。 ?
使得 *
non-greedy,即,它将尝试匹配尽可能少的重复,而不是尽可能多的重复。
在您的示例 re.match('(.*?)word(.*?)',string)
中,这意味着前导 .
越少越好,因此请尝试找到最早的 word
而不是最后一个。尾随 (.*?)
确实毫无意义。