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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…