比如我要分类c*t => CLASS1,和d*g => CLASS2:
Pattern CXT = Pattern.compile("^c.*t$");
Pattern DXG = Pattern.compile("^d.*g$");
public int classify(String in) {
if (CXT.matches(in)) return CLASS1;
if (DXG.matches(in)) return CLASS2;
return -1;
}
如果有很多模式模式,这是非常低效的。
假设所有模式都是正交的,很容易看到一个 DFA 中的单遍就足够了。那么,是否存在可以将所有模式组合在一起的正则表达式处理器?