[Python-bugs-list] [ python-Bugs-695688 ] Problems with non-greedy match groups

SourceForge.net noreply at sourceforge.net
Sun Oct 19 10:21:44 EDT 2003


Bugs item #695688, was opened at 2003-03-01 18:52
Message generated for change (Comment added) made by niemeyer
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=695688&group_id=5470

Category: Regular Expressions
Group: Python 2.2
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Daniel (kamek)
Assigned to: Gustavo Niemeyer (niemeyer)
Summary: Problems with non-greedy match groups

Initial Comment:
The problem is better explained with this code:

import re
regexp = re.compile(r'^begin ((.*?)X)?(.+) end$')

print regexp.search('begin oneXtwo end').groups()
# The above one will correctly print:
#   ('oneX', 'one', 'two')

print regexp.search('begin two end').groups()
# ...but the above one will print:
#   (None, 'two end', 'two')
# If we change regexp's non-greedy
# .*? into .*, it works as it should:
#   (None, None, 'two')


The problem lies in sre; pre correctly matches the non-
greedy version.

----------------------------------------------------------------------

>Comment By: Gustavo Niemeyer (niemeyer)
Date: 2003-10-19 14:21

Message:
Logged In: YES 
user_id=7887

Fixed in Python 2.3.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=695688&group_id=5470



More information about the Python-bugs-list mailing list