nested grouping with regular expressions
Jonathan Gardner
jgardner at jonathangardner.net
Mon May 24 11:24:06 EDT 2004
"Fredrik Lundh" <fredrik at pythonware.com> wrote in message news:<mailman.211.1085382072.6949.python-list at python.org>...
> "ds2000" wrote:
>
> > Unfortunately, re.match("<(..)*>", string).groups() will return only
> > the groups correspond to the last occurence of (regex)
> >
> > Is it possible to get a more tree-like group match (i.e. or at least a
> > list which will contain the groups of each regex occurence).
>
> use re.findall
>
> (you may have to use multiple REs; a re.search to find the entire
> group, a re.findall/finditer to split it up).
>
You'll probably end up not using regular expressions after all. XML
parsers work much better for parsing XML than regexes. In general,
when a regex stops becoming the obvious solution, start using a
parser. ;-)
More information about the Python-list
mailing list