Question about a regular expression
Rob Hodges
s323140 at student.uq.edu.au
Thu Dec 23 12:48:48 EST 1999
"Darrell" <news at dorb.com> writes:
> This doesn't work if you have nested parens.
>
> >>> s='(1(2))'
> >>> print re.search(r"\((.*?)\)",s).groups()
> ('1(2',)
> >>>
True, but it works fine for what Yoav wanted to do, which was to grab
the tokens out of parens that weren't nested (at least in the examples
offered) --
>>> s='a(x,y)b(x)'
>>> for st in re.findall(r"\((.*?)\)", s):
... print string.split(st, ",")
...
['x', 'y']
['x']
If they were nested, we'd need to know more precisely what defines a
token to approach the problem. For example if it were
'a(w,x(y))b(x)', are 'w' and 'x(y)' the tokens belonging to a, and in
turn 'y' the token belonging to x -- or are 'w', 'x' and 'y' what we
want to extract for a? It depends on the meaning of the data and the
detail you want to extract... and frankly, I ain't *touchin'* that
unless I have to ;)
Merry Christmas, Pythonistas!
-Rob
> > > Yoav I H Parish <parish at ikb.mavt.ethz.ch> writes:
> > >
> > > > i have a string which could look something like
> > > >
> > > > a(x,y)b(x)
> > > > or
> > > > c(x,y,z)b(x)a(x,y)
More information about the Python-list
mailing list