我知道反向引用ovverride值,如果发生回溯并且ovverided输出将是新的反向引用。
但如果我以此正则表达式为例:
([abc]+).*\1
然后是字符串:"abc me bca"
输出为:"abc me bca"
有人可以解释这是怎么可能的,因为按照以下步骤:
-[abc] matches a from the input.
-because their is an quantifier + so it will repeate one or more
time and again matches b and c.then stops at whitespace as it's
not either 'a', 'b' or 'c'.
-.* eats all the input string after abc and further goes
to \1(the backreference).
- .* will do backtracking as \1 fails and because .* i.e zero
or more so it will through all the charecters and again the +
of [abc]+ will do backtracking.
-in backtracking of [abc]+ it will be do until 'a' after
removing 'b' and 'c' but still their is no match for
\1 as bca.
那么输出如何变成“abc me bca”..?
答案 0 :(得分:1)
第一个字符是a
,最后一个字符是a
。所以它匹配。
如果abc
被捕获在群组中,然后与bca
进行比较,则会失败。
所以1.Now ab
的引擎回溯将与ca
进行比较。它失败了。所以引擎
将再次回溯。a
与最后a
进行比较并且它会通过。最后引擎
将a
存储在群组中,因为它符合匹配条件。注意\1
是什么
存储在第一组中。它不是固定值。参见演示。