我知道以下事实:如果发生回溯,则回溯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” ..?

最佳答案

第一个字符是a,最后一个字符是a。因此,它是一个匹配项。

发生的情况是abc被捕获到组中,然后与bca进行比较。它失败了。

因此将引擎现在1.cc后退的abca进行比较,失败了。

将再次回溯。a与最后一个a进行比较,然后通过。因此最终引擎

满足匹配条件时将a存储在组中。注意\1

存储在第一组中,它不是固定值,请参见demo。

https://regex101.com/r/sJ9gM7/72

关于java - 正则表达式反向引用的工作方式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29498127/

10-11 10:33