
Brackets in brackets, braces in braces
It is possible to use regular expressions to match nested brackets, if
you FIX the amount of nesting:
Quote:
>>> 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!" ? ;)