Brackets in brackets, braces in braces 
Author Message
 Brackets in brackets, braces in braces

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!',

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. ;)


PS. How about searching line = "??BB!CC!"   ? ;)

Tue, 07 Dec 2004 01:33:04 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Brackets in brackets, braces in braces

2. find mismatching bracket/paren/braces

3. Help: bind on brackets and braces with german Keyboard

4. Brace/Bracket-levels are counted even in comments

5. Braces, Brackets, & Thans

6. More problems with brackets braces and ...

7. Including unmatched brackets in a bracketed string

8. You want braces? I'll give you braces...

9. mixing it up: The role of fractional axis brackets

10. bracket indexing .vs. merge

11. Escaping Square Brackets

12. Nested Brackets Begin { ?


Powered by phpBB® Forum Software