Search code examples
regexbacktracking

Catastrophic backtracking issue


I am running into an issue with a regex pattern when no match is found.

The regex pattern that I use is:

^(?:".*?",){4}"(?:.*?)Cookie:\s(?:.*?)Routing=(.*?);

As test data I use something in the likes of:

"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",

When the 'routing' parameter is found, all works well. However, when the routing parameter is not found by excluding it from the test data, the regex keeps searching. I found out by other posts that this is caused by catastrophic backtracking but I cannot seem to find a way to avoid it.


Solution

  • Narrowing your question to:

    How to avoid catastrophic backtracking?

    Regex side: Be as specific as possible, as Rawing said in his comment, changing ".*?" to "[^"]*" will reduce drastically the number of required backtrack for the engine.

    Input site: When possible, reduce the input to the smallest part you need without loosing information. Here, changing your input from:

    "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",
    

    to

    "POST: /portal/start.asp HTTP/1.1\r\nHost: myhost\r\nCookie: w1n0_er=xxxx; Routing=yyyy;"
    

    and regex to:

    ^".*?Cookie:\s.*?Routing=(.*?);
    

    will help a lot.