Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is it bug or feature in sre?

3 views
Skip to first unread message

Roman Suzi

unread,
Jan 5, 2004, 4:08:33 AM1/5/04
to pytho...@python.org

I've accidentally tried on of my programs written for Python 1.5
and encountered this:

Python 2.3.1 (#1, Sep 25 2003, 10:15:04)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
......
>>> re.compile("(?P<gr>[A-Z]*)?")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/local/lib/python2.3/sre.py", line 179, in compile
return _compile(pattern, flags)
File "/usr/local/lib/python2.3/sre.py", line 229, in _compile
raise error, v # invalid expression
sre_constants.error: nothing to repeat

I wonder why I can't do this? I guess it's of terrible style, but
is this behaviour intentional or not?
Probably it is good idea to mention this in Python update path
documents. Otherwise some old programs will mysteriously fail.


Sincerely yours, Roman A.Suzi
--
- Petrozavodsk - Karelia - Russia - mailto:r...@onego.ru -


Skip Montanaro

unread,
Jan 5, 2004, 7:07:13 AM1/5/04
to Roman Suzi, pytho...@python.org

>>> re.compile("(?P<gr>[A-Z]*)?")
Roman> Traceback (most recent call last):
...
Roman> sre_constants.error: nothing to repeat

Roman> I wonder why I can't do this?

Is there some reason why

"(?P<gr>[A-Z]*)"

or

"(?P<gr>[A-Z]+)?"

isn't sufficient? You specified the zero-or-more repetition inside the
group. There's nothing else to have zero-or-one of.


Roman> I guess it's of terrible style, but is this behaviour intentional
Roman> or not?

I suspect it's intentional, since Fredrik went to the trouble of raising an
exception with a useful error message. <wink>

Roman> Probably it is good idea to mention this in Python update path
Roman> documents. Otherwise some old programs will mysteriously fail.

What were you converting from, an older version of sre, pre, regex, or
(shudder) regexp?

Skip

Roman Suzi

unread,
Jan 5, 2004, 7:19:24 AM1/5/04
to Skip Montanaro, pytho...@python.org
On Mon, 5 Jan 2004, Skip Montanaro wrote:

>
> >>> re.compile("(?P<gr>[A-Z]*)?")
> Roman> Traceback (most recent call last):
> ...
> Roman> sre_constants.error: nothing to repeat
>
> Roman> I wonder why I can't do this?
>
> Is there some reason why
>
> "(?P<gr>[A-Z]*)"

- I want it to be optional, so this is not what I wanted

> or
>
> "(?P<gr>[A-Z]+)?"

- this is right thing

> isn't sufficient? You specified the zero-or-more repetition inside the
> group. There's nothing else to have zero-or-one of.
>
>
> Roman> I guess it's of terrible style, but is this behaviour intentional
> Roman> or not?
>
> I suspect it's intentional, since Fredrik went to the trouble of raising an
> exception with a useful error message. <wink>

And I fully agree with it.

> Roman> Probably it is good idea to mention this in Python update path
> Roman> documents. Otherwise some old programs will mysteriously fail.
>
> What were you converting from, an older version of sre, pre, regex, or
> (shudder) regexp?

Python 1.5.2
re

> Skip


0 new messages