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
Title: Problems with non-greedy match groups
Type: Stage:
Components: Regular Expressions Versions: Python 2.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: niemeyer Nosy List: kamek, niemeyer
Priority: normal Keywords:

Created on 2003-03-01 18:52 by kamek, last changed 2022-04-10 16:07 by admin. This issue is now closed.

Messages (2)
msg14904 - (view) Author: Daniel (kamek) Date: 2003-03-01 18:52
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.
msg14905 - (view) Author: Gustavo Niemeyer (niemeyer) * (Python committer) Date: 2003-10-19 14:21
Logged In: YES 
user_id=7887

Fixed in Python 2.3.
History
Date User Action Args
2022-04-10 16:07:16adminsetgithub: 38077
2003-03-01 18:52:17kamekcreate