正则表达式中Java反向引用的内部实现

时间:2018-10-16 16:14:57

标签: java regex backreference nfa

Java 9中Pattern类的

This文档表明Java确实对正则表达式使用了基于NFA的传统匹配。但是根据Pattern类文档,它也可以在正则表达式中处理backreferences。那么,如果不使用递归回溯实现来处理正则表达式,该如何正确实现呢?

对于某些正则表达式-(正则表达式模式1)\ 1(正则表达式模式2),我的猜测是它将按以下方式完成:

1) Generate NFA1 for regex pattern 1 and NFA2 for regex pattern 2
2) Start matching the input string with NFA1 and if any group is found store
   it. At every point, check if remaining part of the string can match the 
   \1(regex pattern 2) and if it does report a match
3) Else report match not found

对吗?

0 个答案:

没有答案