regexp compilation error

Hans Mulder hansmu at xs4all.nl
Fri Sep 30 14:12:12 CEST 2011


On 30/09/11 11:10:48, Ovidiu Deac wrote:
> I have the following regexp which fails to compile. Can somebody explain why?
>
>>>> re.compile(r"""^(?: [^y]* )*""", re.X)
[...]
> sre_constants.error: nothing to repeat
>
> Is this a bug or a feature?

A feature: the message explains why this pattern is not allowed.

The sub-pattern (?: [^y]* ) matches zero or more non-'y's, so
it potentially matches the empty string.  It you were allowed
to apply '*' to such a sub-pattern, the matcher could go into
an infinite loop, finding billions of matching empty strings,
one after the other.

I'm not sure what you are trying to match, but for zero-or-more
not-'y's, you could write r"^[^y]*".  This is guaranteed to
always match, since there are always at least zero of them.

You might as well write r"^", that is also guaranteed to
always match.

What are you trying to achieve?

-- HansM





More information about the Python-list mailing list