➜

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
标题: re.compile(r'((x|y+)*)*') should not fail
类型: behavior Stage: resolved
Components: Library (Lib), Regular Expressions Versions: Python 3.3, Python 3.4, Python 2.7
process
状态: closed Resolution: fixed
Dependencies: 后续:
分配给: 抄送列表: ezio.melotti, jorendorff, meador.inge, mrabarnett, python-dev, rsc, serhiy.storchaka, timehorse
优先级: normal 关键字: patch

Created on 2008-04-02 17:36 by jorendorff, last changed 2022-04-11 14:56 by admin. This issue is now closed.

文件
文件名 上传时间 Description 编辑
issue-2537.patch meador.inge, 2010-02-11 03:17 patch against 2.7 trunk review
Messages (9)
msg64865 - (view) Author: Jason Orendorff (jorendorff) 日期: 2008-04-02 17:36
Below, the second regexp seems just as guilty as the first to me.

Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04) 
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.compile(r'((x|y)*)*')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py",
line 180, in compile
    return _compile(pattern, flags)
  File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py",
line 233, in _compile
    raise error, v # invalid expression
sre_constants.error: nothing to repeat
>>> re.compile(r'((x|y+)*)*')
<_sre.SRE_Pattern object at 0x18548>

I don't know if that error is to protect the sre engine from bad
patterns or just a courtesy to users.  If the former, it could be a
serious bug.
msg64934 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) 日期: 2008-04-04 17:41
I'm almost tempted to call the first of these a bug:  isn't '((x|y)*)*'
a perfectly valid (albeit somewhat redundant) regular expression?  What
am I missing here?

Even if there are issues with capturing, shouldn't the version without
capturing subexpressions still work?

I get:

>>> re.compile(r'(?:(?:x|y)*)*')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/re.py", line 180, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python2.5/re.py", line 233, in _compile
    raise error, v # invalid expression
sre_constants.error: nothing to repeat
msg64950 - (view) Author: Jason Orendorff (jorendorff) 日期: 2008-04-04 20:37
Huh.  Maybe you're right.  JavaScript, Ruby, and Perl all accept both
regexes, although no two agree on what should be captured:

js> "xyyzy".replace(/((x|y)*)*/, "($1, $2)") 
(xyy, y)zy
js> "xyyzy".replace(/((x|y+)*)*/, "($1, $2)")
(xyy, yy)zy

>> "xyyzy".sub(/((x|y)*)*/, "(\\1, \\2)")
=> "(, y)zy"
>> "xyyzy".sub(/((x|y+)*)*/, "(\\1, \\2)")
=> "(, yy)zy"

  DB<1> $_ = 'xyyzy'; s/((x|y)*)*/(\1 \2)/; print
( )zy
  DB<2> $_ = 'xyyzy'; s/((x|y+)*)*/(\1 \2)/; print
( yy)zy

Ruby's behavior seems best to me.
msg99194 - (view) Author: Meador Inge (meador.inge) * (Python committer) 日期: 2010-02-11 03:17
> Ruby's behavior seems best to me.

We can obtain the Ruby behavior easily.  There is one check in sre_compile.py in the '_simple' function that needs to be removed (see attached patch).  Whether or not the Ruby behavior is the "correct" behavior I am still not sure.  In any case, I think throwing an exception is to aggressive for this case.
msg99237 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) 日期: 2010-02-11 20:49
The re module is addressed in issue #2636.

BTW, my regex module behaves like Ruby:

>>> regex.sub(r"((x|y)*)*", "(\\1, \\2)", "xyyzy", count=1)
'(, y)zy'
>>> regex.sub(r"((x|y+)*)*", "(\\1, \\2)", "xyyzy", count=1)
'(, yy)zy'
msg99248 - (view) Author: Meador Inge (meador.inge) * (Python committer) 日期: 2010-02-12 02:12
> The re module is addressed in issue #2636.

Wow, that issue thread is massive...  What about the 're' module is addressed?  Is 'regex' replacing 're'?  Is 'regex' being rolled into 're'?  Are they both going to exist?
msg99251 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) 日期: 2010-02-12 02:27
The issue started about updating the re module and adding features that other languages already possess in their regex implementations (the last time any significant work was done on it was in 2003).

The hope is that the new regex implementation will eventually replace the existing one, and putting it initially in a module called 'regex' allows it to be tested more easily.

You can do:

import regex as re

and existing code should still work.
msg195662 - (view) Author: Roundup Robot (python-dev) (Python triager) 日期: 2013-08-19 20:30
New changeset 7ab07f15d78c by Serhiy Storchaka in branch '3.3':
Issue #2537: Remove breaked check which prevented valid regular expressions.
http://hg.python.org/cpython/rev/7ab07f15d78c

New changeset f4271cc2dfb5 by Serhiy Storchaka in branch 'default':
Issue #2537: Remove breaked check which prevented valid regular expressions.
http://hg.python.org/cpython/rev/f4271cc2dfb5

New changeset 7b867a46a8b4 by Serhiy Storchaka in branch '2.7':
Issue #2537: Remove breaked check which prevented valid regular expressions.
http://hg.python.org/cpython/rev/7b867a46a8b4
msg195664 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) 日期: 2013-08-19 20:43
This issue is a duplicate of issue1633953. See also issue18647. After some fixes in other parts of the re module this check has become even more invalid.
历史
日期 用户 动作 参数
2022-04-11 14:56:32admin修改github: 46789
2013-08-19 20:43:29serhiy.storchaka修改状态: open -> closed
versions: + Python 3.3
标题: re.compile(r'((x|y+)*)*') should fail -> re.compile(r'((x|y+)*)*') should not fail
消息: + msg195664

resolution: fixed
stage: resolved
2013-08-19 20:33:07serhiy.storchaka链接issue1633953 superseder
2013-08-19 20:30:24python-dev修改抄送: + python-dev
消息: + msg195662
2012-11-27 22:47:00ezio.melotti修改抄送: + serhiy.storchaka
2012-11-25 15:47:41ezio.melotti修改components: + Regular Expressions
2012-11-25 15:12:53mark.dickinson修改抄送: - mark.dickinson
2012-11-25 14:23:22Ramchandra Apte修改components: + Library (Lib), - Regular Expressions
versions: + Python 3.4
2010-02-12 02:27:16mrabarnett修改消息: + msg99251
2010-02-12 02:12:23meador.inge修改抄送: mark.dickinson, rsc, timehorse, jorendorff, ezio.melotti, mrabarnett, meador.inge
type: behavior
消息: + msg99248
components: + Regular Expressions
2010-02-11 20:49:44mrabarnett修改抄送: + mrabarnett
消息: + msg99237
2010-02-11 03:17:57meador.inge修改文件: + issue-2537.patch

抄送: + meador.inge
消息: + msg99194

keywords: + patch
2009-05-12 14:25:39ezio.melotti修改抄送: + ezio.melotti
2008-09-28 19:23:32timehorse修改抄送: + timehorse
versions: + Python 2.7, - Python 2.6
2008-04-24 21:02:18rsc修改抄送: + rsc
2008-04-04 20:37:57jorendorff修改消息: + msg64950
2008-04-04 17:41:12mark.dickinson修改抄送: + mark.dickinson
消息: + msg64934
2008-04-02 17:36:05jorendorff创建