[issue14991] Option for regex groupdict() to show only matching names
Raymond Hettinger
report at bugs.python.org
Sun Jun 3 19:01:01 CEST 2012
New submission from Raymond Hettinger <raymond.hettinger at gmail.com>:
Currently, mo.groupdict() always inserts a default value for unmatched named groups. This is helpful in some use cases and awkward in others.
I propose adding an option to suppress default entries:
>>> # CURRENT WAY
>>> pattern = r'(?P<TITLE>Mrs |Mr |Dr )?(?P<LASTNAME>\w+)(?P<SUFFIX> Phd| JD)?'
>>> print re.match(pattern, 'Dr Who').groupdict()
{'LASTNAME': 'Who', 'SUFFIX': None, 'TITLE': 'Dr '}
>>> # PROPOSED WAY
>>> print re.match(pattern, 'Dr Who').groupdict(nodefault=True)
{'LASTNAME': 'Who', 'TITLE': 'Dr '}
>>> # UPSTREAM VARIANT
>>> print re.match(pattern, 'Dr Who', re.NODEFAULT).groupdict()
{'LASTNAME': 'Who', 'TITLE': 'Dr '}
There is probably a better name than "nodefault", but I would like to see someway to improve the usability of groupdict().
----------
messages: 162223
nosy: rhettinger
priority: normal
severity: normal
status: open
title: Option for regex groupdict() to show only matching names
type: enhancement
versions: Python 3.4
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14991>
_______________________________________
More information about the Python-bugs-list
mailing list