regexp compilation error

Ovidiu Deac ovidiudeac at
Fri Sep 30 05:57:25 EDT 2011

This is only part of a regex taken from an old perl application which
we are trying to understand/port to our new Python implementation.

The original regex was considerably more complex and it didn't compile
in python so I removed all the parts I could in order to isolate the
problem such that I can ask help here.

So the problem is that this regex doesn't compile. On the other hand
I'm not really sure it should. It's an anchor on which you apply *.
I'm not sure if this is legal.

On the other hand if I remove one of the * it compiles.

>>> re.compile(r"""^(?: [^y]* )*""", re.X)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/", line 190, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python2.6/", line 245, in _compile
    raise error, v # invalid expression
sre_constants.error: nothing to repeat
>>> re.compile(r"""^(?: [^y] )*""", re.X)
<_sre.SRE_Pattern object at 0x7f4069cc36b0>
>>> re.compile(r"""^(?: [^y]* )""", re.X)
<_sre.SRE_Pattern object at 0x7f4069cc3730>

Is this a bug in python regex engine? Or maybe some incompatibility with Perl?

On Fri, Sep 30, 2011 at 12:29 PM, Chris Angelico <rosuav at> wrote:
> On Fri, Sep 30, 2011 at 7:26 PM, Ovidiu Deac <ovidiudeac at> wrote:
>> $ python --version
>> Python 2.6.6
> Ah, I think I was misinterpreting the traceback. You do actually have
> a useful message there; it's the same error that my Py3.2 produced:
> sre_constants.error: nothing to repeat
> I'm not sure what your regex is trying to do, but the problem seems to
> be connected with the * at the end of the pattern.
> ChrisA
> --

More information about the Python-list mailing list