Detect if a regexp is exponential

前端 未结 4 580
梦如初夏
梦如初夏 2021-01-05 04:12

This article show that there is some regexp that is O(2^n) when backtracking. The example is (x+x+)+y. When attempt to match a string like xxxx...p it going to

4条回答
  •  清酒与你
    2021-01-05 04:28

    Any regex without backreferences can be matched in linear time, though many regex engines out there in the real world don't do it that way (at least many regex engines that are plugged into programming language runtime environments support backreferences, and don't switch to a more efficient execution model when no backreferences are present).

    There's no easy way to find out how much time a regex with backreferences is going to consume.

提交回复
热议问题