Skip to content Skip to sidebar Skip to footer

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 section 1.2 random 2 1.2 random 2 1.2. random 2 random 2 random 2. But its

Solution 1:

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 randomrandomrandomrandomrandommatch on section 1.2random2match on 1.2random2match on 1.2. random2match on random2match on random2.
non match on random

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

Post a Comment for "Regex Taking Too Long In Python"