regex help for a newbie
Tobiah
toby at rcsreg.com
Fri Apr 9 11:14:36 EDT 2004
>
> 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 %().
#!/usr/bin/python
### I realize that this will not serve you in all of the cases
### that you are likely to need to handle, but just to show
### that the case that you mention can be handled with regular
### expressions, I submit the following:
import re
string = "aaa%(BBB%(CCC)BBB)aaa%(DDD)aaa"
m = re.search('([^%]*)%\(([^%]*)%\(([^)]*)\)([^)]*)\)([^)]+)%\(([^)]*)\)(.*)', string)
print (m.groups())
### This yields:
###
### ('aaa', 'BBB', 'CCC', 'BBB', 'aaa', 'DDD', 'aaa')
###
###
###
### Tobiah
More information about the Python-list
mailing list