Python 're' bad work case

A Sublime package that I use for autocompletion in lua uses the Python 're' module, but one regular expression causes severe recessions. Here is a minimal example:

import re
rx = re.compile(r"\bfunction(?:\s+[a-zA-Z0-9._]*)?\(((?:[a-zA-Z_][a-zA-Z0-9_]*|\.\.\.|,\s*)*)\)")
rx.match('function f(aaaaaaa, bbbbbbbb, cccccccc, ddddddd eeeeee)')  # Very slow
rx.match('function f(aaaaaaa, bbbbbbbb, cccccccc, ddddddd, eeeeee)')  # Adding a comma between the last function arguments in the string fixes it.

I am not in my depths with debugging regular expressions, but it seems appropriate , although it is above my head.

Does anyone know an equivalent model that I can use, but which has good performance?

Thank!

+4
source share
2 answers

, * -quantified * quantifier: (?:[a-zA-Z_][a-zA-Z0-9_]*|\.\.\.|,\s*)*. . () , .

Python re .

:

\bfunction(?:\s+[a-zA-Z0-9._]*)?\(((?:(?=([a-zA-Z_][a-zA-Z0-9_]*))\2|\.\.\.|,\s*)*)\)
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                  

[a-zA-Z_][a-zA-Z0-9_]* , lookbehind. (?=([a-zA-Z_][a-zA-Z0-9_]*))\2 ( ) [a-zA-Z_][a-zA-Z0-9_]*, ( [a-zA-Z_][a-zA-Z0-9_]*+ (?>[a-zA-Z_][a-zA-Z0-9_]*+)). "" \2 [a-zA-Z_][a-zA-Z0-9_]*.

regex.

, , re.search, \b. , re.match (= ).

+1

(?:[a-zA-Z_][a-zA-Z0-9_]*|\.\.\.|,\s*)*, , .

, * . Verbose, :

re.compile(r'''\bfunction\b \s*[a-zA-Z0-9._]*
\(

(
  (?:
    (?:[a-zA-Z_][a-zA-Z0-9_]*|\.\.\.)
    (?: ,\s* (?:[a-zA-Z_][a-zA-Z0-9_]*|\.\.\.) )*
  )?
)

\)''', re.VERBOSE)

(*): , (a*|b)* a*(ba*)*

+1

All Articles