当找不到匹配项时,我遇到了正则表达式模式的问题。
我使用的正则表达式模式是:
"key=value" - GET request
{"key" : "value"} - POST request
作为测试数据,我使用以下内容:
^(?:".*?",){4}"(?:.*?)Cookie:\s(?:.*?)Routing=(.*?);
当'路由'参数找到,一切正常。但是,如果通过从测试数据中排除路由参数找不到路由参数,则正则表达式会继续搜索。我发现其他帖子说这是由灾难性的回溯造成的,但我似乎无法找到避免它的方法。
答案 0 :(得分:2)
将您的问题缩小为:
如何避免灾难性的回溯?
正则表达方面:尽可能具体,正如Rawing在评论中所说,将".*?"
更改为"[^"]*"
会大幅减少引擎所需的回溯次数。
输入站点:如果可能,将输入减少到所需的最小部分,而不会丢失信息。在这里,改变您的输入:
"a","b","c","d","POST: /portal/start.asp HTTP/1.1\r\nHost: myhost\r\nCookie: w1n0_er=xxxx; routxing=yyyy;"x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x",
到
"POST: /portal/start.asp HTTP/1.1\r\nHost: myhost\r\nCookie: w1n0_er=xxxx; Routing=yyyy;"
和正则表达式:
^".*?Cookie:\s.*?Routing=(.*?);
会有很多帮助。