[Python-ideas] Re module repeat

Christopher King g.nius.ck at gmail.com
Mon Aug 1 02:36:05 CEST 2011


Dear Idealists,
    I notice that when you do use the re module grouping, that it only tells
you what it matched last:

Dumb Real Python Code:
>>> import re
>>> match=re.search('^(?P<letter>[a-z])*$', 'abcz')
>>> match.groupdict()
{'letter': '0'}
What happened to all the other matches. Now here is a cool idea.

Cool Improved Python Code
>>> import re
>>> match=re.search('^(?P<letter>[a-z])*$', 'abcz')
>>> match.groupdict()
{'number': '0'}
{'letter.0':'a', 'letter.1':'b', 'letter.2':'c', 'letter.3':'z'}
*
*

Now, we see all that it matched. Now the problem with this and all ideas is
reverse compatibility. So an addition is also too.
>>> import re
>>> match=re.search('^(?P*P*<number>[a-z])** and also
(?PP=letter.0)(?PP=letter.-1)*$', 'abcz* and also az*')
>>> match.groupdict()
{'letter.0':'a', 'letter.1':'b', 'letter.2':'c', 'letter.3':'z'}

Notice how I added an extra P. I also made it so that matching it in the
text is also more adaptable. Please consider this idea.

Sincerely,
    Me
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110731/770e4d86/attachment.html>


More information about the Python-ideas mailing list