suggestion for module re
Skip Montanaro
skip at pobox.com
Mon Oct 22 10:02:49 EDT 2001
Jose> My first goal is to get all the *named* fields of a regex, for all
Jose> of the matches in a source string. The re.RegexObject.findall
Jose> method is "almost" good, but it returns a tuple of matched
Jose> *groups*, leading to a dumb pharentesis-counting task and to code
Jose> hard to maintain.
I suspect that in most situations you will want all the interesting groups
to be named or none of the interesting groups to be named. If you want
named groups, use the "(?:...)" construct to create "throwaway" groups. See
the re syntax docs for more info:
http://www.python.org/doc/current/lib/re-syntax.html
Your example would thus become:
rx = re.compile(r'(?:(?P<a>a|A)(?P<b>b|B))')
(though why you needed the outer parens in the first place is not clear).
--
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/
More information about the Python-list
mailing list