简体   繁体   中英

Regex taking too long in python

I have used regex101 to test my regex and it works fine.What i am trying to is to detect these patterns

  1. section 1.2 random 2
  2. 1.2 random 2
  3. 1.2. random 2
  4. random 2
  5. random 2.

But its just random it shouldn't match if the string is like that

  1. random

My regex is this.

  m = re.match(r"^(((section)\s*|(\d+\.)|\d+|(\d+\.\d+)|[a-zA-z\s]|[a-zA-z\.\s])+((\d+\.$)|\d+$|(\d+\.\d+$)))","random random random random random",flags = re.I)

If i give in a long string it gets stuck.Any ideas?

After some simplification, this regular expression meets the requirements stated above and reproduced in the test cases below.

import re

regex = r'(?:section)*\s*(?:[0-9.])*\s*random\s+(?!random)(?:[0-9.])*'

strings = [
   "random random random random random",
   "section 1.2 random 2",
   "1.2 random 2",
   "1.2. random 2",
   "random 2",
   "random 2.",
   "random",
]

for string in strings:
    m = re.match(regex, string, flags = re.I)
    if m:
        print "match on", string
    else:
        print "non match on", string

which gives an output of:

non match on random random random random random
match on section 1.2 random 2
match on 1.2 random 2
match on 1.2. random 2
match on random 2
match on random 2.
non match on random

See it in action at: https://eval.in/661183

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