[New-bugs-announce] [issue17087] Improve the repr for regular expression match objects
report at bugs.python.org
Thu Jan 31 00:53:02 CET 2013
New submission from Raymond Hettinger:
Experience teaching Python has shown that people have a hard time learning to work with match objects. A contributing cause is the opaque repr:
>>> import re
>>> s = 'On 3/14/2013, Python celebrate Pi day.'
>>> mo = re.search(r'\d+/\d+/\d+', s)
<_sre.SRE_Match object at 0x100456100>
They could explore the match object with dir() and help() and the matchobject methods and attributes:
['__class__', '__copy__', '__deepcopy__', ...
'end', 'endpos', 'expand', 'group', ... ]
However, this gets old when experimenting with alternative regular expressions. A better solution is to improve the repr:
>>> re.search(r'\d+/\d+/\d+', s)
<SRE Match object: start=3, stop=12, group(0)='3/14/2013'>
This would make the regular expression module much easier to work with.
components: Library (Lib)
title: Improve the repr for regular expression match objects
versions: Python 3.4
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce