regex help for a newbie

Marco Herrn herrn at gmx.net
Mon Apr 5 08:47:34 EDT 2004


Hi,

I am not very familiar with regular expressions. So I hope someone can
help me to achieve what I want.

I have the following string in my program:

  string= "aaa%(BBB%(CCC)BBB)aaa%(DDD)aaa"

Now I need to extract the parts that are enclosed in %().
There are 3 levels of nesting. The first level is named 
'aaa', the second 'BBB' and 'DDD' and the third 'CCC'.
I do not need to extract the third level at this moment, since I extract
the parts in a recursive function. So the thing I want to achieve here
is to extract %(BBB%(CCC)BBB) and %(DDD).

I tried it with the following:

  re.search("%\(.*\)", string).group()

But that returns:
  
  %(BBB%(CCC)BBB)aaa%(DDD)'

which is, of course, not what I want.
So how must the regex look like that I get the two strings I need?


Marco


-- 
Marco Herrn             herrn at gmx.net
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736




More information about the Python-list mailing list