nested grouping with regular expressions

D

ds2000

Hi,

Let regex be some regular expression, which contains groups.

The pattern that I am interested in is the form of "<(regex)*>" (i.e.
the list of some constantly repeating regular expression inside the <>
brackets.

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).

Thanks ahead,
ds
 
F

Fredrik Lundh

ds2000 said:
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).

</F>
 
J

Jonathan Gardner

Fredrik Lundh said:
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. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top