Detect if a regexp is exponential

前端 未结 4 604
梦如初夏
梦如初夏 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:35

    You could detect and reject nested repetitions using a regex parser, which corresponds to a star height of 1. I've just written a module to compute and reject start heights of >1 using a regex parser from npm.

    $ node safe.js '(x+x+)+y'
    false
    $ node safe.js '(beep|boop)*'
    true
    $ node safe.js '(a+){10}'
    false
    $ node safe.js '\blocation\s*:[^:\n]+\b(Oakland|San Francisco)\b'
    true
    

提交回复
热议问题