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

SourceForge.net noreply@sourceforge.net
Sun, 22 Jun 2003 17:10:57 -0700


Bugs item #695688, was opened at 2003-03-01 18:52
Message generated for change (Settings changed) 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: Open
Resolution: None
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.

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

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