Complexity of Regex substitution

前端 未结 8 1280
傲寒
傲寒 2020-12-01 16:58

I didn\'t get the answer to this anywhere. What is the runtime complexity of a Regex match and substitution?

Edit: I work in python. But would like to know in genera

相关标签:
8条回答
  • 2020-12-01 17:27

    In python's re library, even if a regex is compiled, the complexity can still be exponential (in string length) in some cases, as it is not built on DFA. Some references here, here or here.

    0 讨论(0)
  • 2020-12-01 17:32

    To delve into theprise's answer, for the construction of the automaton, O(2^m) is the worst case, though it really depends on the form of the regular expression (for a very simple one that matches a word, it's in O(m), using for example the Knuth-Morris-Pratt algorithm).

    0 讨论(0)
提交回复
热议问题