non-terminating regex match

Maurizio Vitale mav at cuma.polymath-solutions.lan
Wed Apr 2 22:16:47 EDT 2008


MRAB <google at mrabarnett.plus.com> 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,

       Maurizio



More information about the Python-list mailing list