Which regular expression requires backtracking?

假装没事ソ 提交于 2020-02-25 17:09:30

问题


There are three different solutions to implement regular expression matching: DFA, NFA and Backtracking. I am looking for examples:

  • a regexp, which can be solved with a DFA and the reason, why a DFA is sufficient.
  • a regexp, which requires a NFA and the reason why a NFA is necessary.
  • a regexp, which requires backtracking and the reason why backtracking is necessary.

A recommendation for some good literature about this topic would be nice, too.


回答1:


i guess there is more than 1 meaning to the word backtracking - even '.*a' has to backtrack to match the string "lalaiiiiiii" (because .* will first match the whole string - so then a won't match anything - and only then it will give up one character at a time, so the final match would be "lala")

i highly recommend http://www.regular-expressions.info/




回答2:


What I found out so far is:

  • Every regular expression, which can be implemented with a NFA, can also be implemented with a DFA. Every NFA can be transformed into a DFA.

  • Regular expressions, which require backtracking, are regular expressions, which contain back references like /(a)\1/.



来源:https://stackoverflow.com/questions/8132412/which-regular-expression-requires-backtracking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!