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

How can I find strings like this with regular expressions:\something
[something1] {something2} {something3}Where in the braces can be also
another braces. For example:\frac{1\times10^{-16}}{4}   can be at the
place, where something2 is.(braces in pairs)\interval [J]
{\frac{1\times10^{-16}}{4}} {1..5}My half solution is at
www.szgti.bmf.hu/~ahorvath/regexp.txt.


Sat, 02 Oct 2004 23:30:05 GMT  
 Brackets in brackets, braces in braces

    Arpad> How can I find strings [using regular expressions] ... Where in
    Arpad> the braces can be also another braces.

In general, you can't.  As Tim Peters and others have pointed out in the
past, regular expressions can't count.  You need a more powerful parser than
re's to properly handle nested constructs.

--



Sun, 03 Oct 2004 01:17:40 GMT  
 Brackets in brackets, braces in braces

Quote:

>How can I find strings like this with regular expressions:\something
>[something1] {something2} {something3}Where in the braces can be also
>another braces. For example:\frac{1\times10^{-16}}{4}   can be at the
>place, where something2 is.(braces in pairs)\interval [J]
>{\frac{1\times10^{-16}}{4}} {1..5}My half solution is at
>www.szgti.bmf.hu/~ahorvath/regexp.txt.

You can't do it with re but, if I recall correctly, someone posted a
link to a class based on SPITBOL/SNOBOL's pattern matching engine a
little while ago.

That would have no trouble with nested structures.

--
Dale Strickland-Clark
Riverhall Systems Ltd



Sun, 03 Oct 2004 03:21:05 GMT  
 Brackets in brackets, braces in braces

Quote:
>How can I find strings like this with regular expressions:\something
>[something1] {something2} {something3}Where in the braces can be also
>another braces. For example:\frac{1\times10^{-16}}{4}   can be at the
>place, where something2 is.(braces in pairs)\interval [J]
>{\frac{1\times10^{-16}}{4}} {1..5}My half solution is at
>www.szgti.bmf.hu/~ahorvath/regexp.txt.

Depending on what your real goal is, you might want to play with
using split and making an actual nested list structure.
E.g., I posted this for a similar question in the past (it just does one
kind of brackets, i.e, braces):

Quote:
> >>> s = "A line of text with {multiple and {nested} braces} to {confuse} re!"
> >>> sl = eval('["'+'"], "'.join('", ["'.join(s.split('{')).split('}'))+'"]')
> >>> sl
> ['A line of text with ', ['multiple and ', ['nested'], ' braces'], ' to ', ['confuse'], ' re!']
>then walk the tree as desired.

The above uses eval to do the nest counting. You'd have to split on '[' and ']' first, and then
walk the tree and do '{' and '}' processing similarly to expand the first tree's elements, if
you had both kinds of bracketing. Plus you'd want to preserve some kind of indication of what
kind of bracket your final nested lists represented.

Or you could write a recursive parser. Might be fun ;-)
Or you could use a python flex workalike to do it, I suppose. I haven't tried one yet.

Regards,
Bengt Richter



Sun, 03 Oct 2004 07:04:48 GMT  
 
 [ 4 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