Brackets in brackets, braces in braces
Pekka Niiranen
krissepu at vip.fi
Thu Jun 20 13:33:04 EDT 2002
It is possible to use regular expressions to match nested brackets, if
you FIX the amount of nesting:
>>> pattern = re.compile("(\?[^?!]+(\?[^?!]+\!)*[^?!]+\!)")
>>> Line = "?AA?BB!CC!?DD!ee?EE!ff?FF?GG!HH!"
>>> print re.findall(pattern, Line)
[('?AA?BB!CC!', '?BB!'), ('?DD!', ''), ('?EE!', ''), ('?FF?GG!HH!',
'?GG!')]
In above I am searching strings limited by ?- and ! -characters.
Only one level of nesting is allowed.
The only problem I find is that re.findall() returns empty matches too.
Maybe in next version of SRE modified findall is introduced. ;)
-pekka-
PS. How about searching line = "??BB!CC!" ? ;)
More information about the Python-list
mailing list