New submission from Raymond Hettinger <raymond.hettin...@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 <rep...@bugs.python.org> <http://bugs.python.org/issue14991> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com