简体   繁体   中英

Python Regex search taking too long

Following regex comparison is taking too long (> 2 mins).

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

Removing the length limits ({2-50}), solves the issue.

What is the error in the pattern?

env: Ubuntu i5 4GB Python 2.7.3

(\S+){2,50}

Are you sure you need this? \\S+ means one or more occurrences. And then you want 2-50 occurrences of it?

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/'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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