正则表达式中最短的匹配结束 [英] Shortest match in regex from end
问题描述
给定一个输入字符串 fooxxxxxxfooxxxboo
我正在尝试编写一个匹配 fooxxxboo
的正则表达式,即从第二个 foo 开始到最后一个 boo.
Given an input string fooxxxxxxfooxxxboo
I am trying to write a regex that matches fooxxxboo
i.e. starting from the second foo till the last boo.
我尝试了以下
foo.*?boo
匹配完整的字符串 fooxxxxxxfooxxxboo
foo.*boo
也匹配完整的字符串 fooxxxxxxfooxxxboo
foo.*boo
also matches the complete string fooxxxxxxfooxxxboo
我读了这篇贪婪与不情愿与占有量词 并且我理解它们的区别,但我试图从匹配正则表达式的末尾匹配最短的字符串,即类似于要从后面评估的正则表达式的东西.有什么办法可以只匹配最后一部分吗?
I read this Greedy vs. Reluctant vs. Possessive Quantifiers and I understand their difference, but I am trying to match the shortest string from the end which matches the regex i.e. something like the regex to be evaluated from back. Is there any way I can match only the last portion?
推荐答案
使用 否定前瞻断言一>.
foo(?:(?!foo).)*?boo
(?:(?!foo).)*?
- 任何字符的非贪婪匹配,但不是 foo
零次或多次.也就是说,在匹配每个字符之前,它会检查字符是否不是字母f
后跟两个o
.如果是,则只匹配相应的字符.
(?:(?!foo).)*?
- Non-greedy match of any character but not of foo
zero or more times. That is, before matching each character, it would check that the character is not the letter f
followed by two o
's. If yes, then only the corresponding character will be matched.
为什么正则表达式 foo.*?boo
匹配完整的字符串 fooxxxxxxfooxxxboo
?
Why the regex foo.*?boo
matches the complete string fooxxxxxxfooxxxboo
?
因为您的正则表达式中的第一个 foo
匹配 foo
字符串和以下 .*?
将进行非贪婪匹配字符串 boo
,所以我们得到了两个匹配项 fooxxxxxxfooxxxboo
和 fooxxxboo
.因为第二个匹配出现在第一个匹配中,正则表达式引擎只显示第一个.
Because the first foo
in your regex matches both the foo
strings and the following .*?
will do a non-greedy match upto the string boo
, so we got two matches fooxxxxxxfooxxxboo
and fooxxxboo
. Because the second match present within the first match, regex engine displays only the first.
这篇关于正则表达式中最短的匹配结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!