gpt4 book ai didi

regex - 贪婪 vs. 不情愿 vs. 占有 限定词

转载 作者:行者123 更新时间:2023-12-03 03:58:19 25 4
gpt4 key购买 nike

我找到了这个tutorial关于正则表达式,虽然我直观地理解“贪婪”、“不情愿”和“占有”限定符的作用,但我的理解似乎存在严重漏洞。

具体来说,在以下示例中:

Enter your regex: .*foo // Greedy qualifier
Enter input string to search: xfooxxxxxxfoo
I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13.

Enter your regex: .*?foo // Reluctant qualifier
Enter input string to search: xfooxxxxxxfoo
I found the text "xfoo" starting at index 0 and ending at index 4.
I found the text "xxxxxxfoo" starting at index 4 and ending at index 13.

Enter your regex: .*+foo // Possessive qualifier
Enter input string to search: xfooxxxxxxfoo
No match found.

解释提到整个输入字符串,字母被消耗,匹配器后退,最右边出现的“foo”已经<强>反流等

不幸的是,尽管有很好的比喻,我仍然不明白谁吃什么......你知道另一个教程可以(简洁地)解释正则表达式引擎如何工作吗?

或者,如果有人可以用稍微不同的措辞解释以下段落,我们将不胜感激:

The first example uses the greedy quantifier .* to find "anything", zero or more times, followed by the letters "f", "o", "o". Because the quantifier is greedy, the .* portion of the expression first eats the entire input string. At this point, the overall expression cannot succeed, because the last three letters ("f", "o", "o") have already been consumed [by whom?]. So the matcher slowly backs off [from right-to-left?] one letter at a time until the rightmost occurrence of "foo" has been regurgitated [what does this mean?], at which point the match succeeds and the search ends.

The second example, however, is reluctant, so it starts by first consuming [by whom?] "nothing". Because "foo" doesn't appear at the beginning of the string, it's forced to swallow [who swallows?] the first letter (an "x"), which triggers the first match at 0 and 4. Our test harness continues the process until the input string is exhausted. It finds another match at 4 and 13.

The third example fails to find a match because the quantifier is possessive. In this case, the entire input string is consumed by .*+ [how?], leaving nothing left over to satisfy the "foo" at the end of the expression. Use a possessive quantifier for situations where you want to seize all of something without ever backing off [what does back off mean?]; it will outperform the equivalent greedy quantifier in cases where the match is not immediately found.

最佳答案

我会尝试一下。

贪婪量词首先尽可能多地匹配。因此 .* 匹配整个字符串。然后匹配器尝试匹配后面的 f,但没有剩余字符。因此它“回溯”,使贪婪量词少匹配一个字符(使字符串末尾的“o”不匹配)。这仍然与正则表达式中的 f 不匹配,因此它又回溯了一步,使贪婪量词再次少匹配一个字符(使字符串末尾的“oo”不匹配)。 still 与正则表达式中的 f 不匹配,因此它又回溯了一步(使字符串末尾的“foo”保持不匹配)。现在,匹配器最终匹配正则表达式中的 f,并且 o 和下一个 o 也匹配。成功!

不情愿或“非贪婪”量词首先匹配尽可能少的内容。因此 .* 首先不匹配任何内容,从而使整个字符串不匹配。然后匹配器尝试匹配后面的 f,但字符串中不匹配的部分以“x”开头,因此不起作用。因此,匹配器回溯,使非贪婪量词再匹配一个字符(现在它匹配“x”,使“fooxxxxxxfoo”不匹配)。然后它尝试匹配 f,结果成功,并且正则表达式中的 o 和下一个 o 也匹配。成功!

在您的示例中,它然后使用字符串的剩余不匹配部分“xxxxxxfoo”重新启动该过程,遵循相同的过程。

占有量词就像贪婪量词一样,但它不会回溯。因此它以 .* 开始匹配整个字符串,不留下任何不匹配的内容。那么它就没有任何东西可以与正则表达式中的 f 匹配。由于所有格量词不会回溯,因此匹配失败。

关于regex - 贪婪 vs. 不情愿 vs. 占有 限定词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5319840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com