non-terminating regex match

Maurizio Vitale mav at cuma.polymath-solutions.lan
Thu Apr 3 04:16:47 CEST 2008

MRAB <google at> writes:

> I think the problem is with this bit: '(?:(?:::)?\w+)*'. The '::' is
> optional and also in a repeated group, so if it tries to match, say,
> 'abc' it can try and then backtrack all of these possibilities: abc,
> ab c, a bc, a b c. The longer the string, the more possibilities to
> try. Try this instead:
>     r = re.compile (
>         r'(?P<scope>(?:::)?(?:\w+::)*)?'
>         r'(?P<name>\w+)'
>         r'(?:\((?P<arguments>[^\)]*)\))?'
>         )

That was indeed the problem. Your version is ok w/ the minor difference
that the named group <scope> includes the '::' at the end. This can be
easily stripped off. Or maybe the regexp can be massaged further.
Thanks a lot,


More information about the Python-list mailing list