我正在使用Apache Regexp作为正则表达式验证的库。我想知道如何跟踪一些正则表达式是否导致了灾难性回溯。
我想知道是否有一些技巧来捕捉哪个正则表达式和字符串值导致了灾难性回溯?
我尝试在RE.java类中进行了一些修改,但效果不如预期。
这是我的修改:
public RE(String pattern) throws RESyntaxException
{
this(pattern, MATCH_NORMAL);
paramString = pattern;
}
public RE(String pattern, int matchFlags) throws RESyntaxException
{
this(new RECompiler().compile(pattern), matchFlags);
paramString = pattern;
}
int callcounterMN = 0;
protected int matchNodes(int firstNode, int lastNode, int idxStart)
{
callcounterMN++;
if (callcounterMN == 100) {
try {
String pc1 = new Exception().getStackTrace()[5].getClassName();
if (pc1.indexOf("UpdateWebForm") > 1)
System.out.println("regex loop reach "+callcounterMN+" with regex : "+paramString+" "+this.search.substring(0));
} catch (Exception e) {}
}