简体   繁体   English

Python Regex搜索花费的时间太长

[英]Python Regex search taking too long

Following regex comparison is taking too long (> 2 mins). 进行正则表达式比较后花费的时间太长(> 2分钟)。

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

Removing the length limits ({2-50}), solves the issue. 删除长度限制({2-50})解决了该问题。

What is the error in the pattern? 模式中有什么错误?

env: Ubuntu i5 4GB Python 2.7.3 环境:Ubuntu i5 4GB Python 2.7.3

(\S+){2,50}

Are you sure you need this? 您确定需要这个吗? \\S+ means one or more occurrences. \\S+表示一个或多个事件。 And then you want 2-50 occurrences of it? 然后您想要2-50次出现吗?

Why not: 为什么不:

\S{2,50}

why not make it much simpler... 为什么不简化它呢?

re.match('([^/]+)/([^/]+)-trailing/', 'test-request/this-is-crashing/')

although in this case it does not find anything... 尽管在这种情况下它什么也没找到...

i suppose you want to catch only strings which are similar to this: 我想您只想捕获类似于以下内容的字符串:

'<SOME-TEXT>/<SOME-TEXT>-trailing/'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM