Python Regex搜索耗时太长

Ank*_*pta 0 python regex

正则表达式比较后需要太长时间(> 2分钟).

re.search('^(\S+){2,50}/(\S+){2,50}\-trailing/$', 'test-request/this-is-crashing/')
Run Code Online (Sandbox Code Playgroud)

删除长度限制({2-50})可解决问题.

模式中的错误是什么?

环境:Ubuntu i5 4GB Python 2.7.3

war*_*iuc 8

(\S+){2,50}
Run Code Online (Sandbox Code Playgroud)

你确定需要这个吗?\S+表示一次或多次出现.然后你想要2-50次出现吗?

为什么不:

\S{2,50}
Run Code Online (Sandbox Code Playgroud)

  • 你是对的,这被称为[灾难性的回溯](http://www.regular-expressions.info/catastrophic.html) (4认同)