PHP: unexpected PREG_BACKTRACK_LIMIT_ERROR

前端 未结 1 1899
盖世英雄少女心
盖世英雄少女心 2020-12-06 21:02

function recursiveSplit($string, $layer) {
    $err = preg_match_all(\"/\\{(([^{}]*|(?R))*)\\}/\",$string,$matches);
    echo \"Elementi trovati: $err
相关标签:
1条回答
  • 2020-12-06 21:48

    Another classic case of catastrophic backtracking. Must be my lucky day today.

    /\{(([^{}]*|(?R))*)\}/
    

    only matches if the braces are nested correctly. Which they aren't, of course, in your string.

    Now the problem is that your regex needs to figure out all possible string combinations you can build with 106 as to figure that out because you have nested quantifiers ((...)*)*). Which (correct me if I'm wrong) should be somewhere in the vicinity of 106! which comes to

    114628056373470835453434738414834942870388487424139673389282723476762012382449946252660360871841673476016298287096435143747350528228224302506311680000000000000000000000000

    which easily beats your PREG_BACKTRACK_LIMIT.

    If you use possessive quantifiers to make sure that you'll never backtrack into the non-braces you've already matched, then you should be OK:

    /\{(([^{}]*+|(?R))*)\}/
    
    0 讨论(0)
提交回复
热议问题