问题
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.
I tried the following
foo.*?boo
matches the complete string 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?
回答1:
Use negative lookahead assertion.
foo(?:(?!foo).)*?boo
DEMO
(?:(?!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.
Why the regex foo.*?boo
matches the complete string fooxxxxxxfooxxxboo
?
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.
回答2:
.*(foo.*?boo)
Try this. Grab the capture i.e $1
or \1
.
See demo.
https://regex101.com/r/nL5yL3/9
来源:https://stackoverflow.com/questions/27396781/shortest-match-in-regex-from-end