Posted to tcl by kbk at Sat Feb 26 18:45:46 GMT 2011view raw

  1. set text {And what is your definition of "between {{ and }}" {{in {{a string}} {{like this}} one}}?}
  2.  
  3. set depth 0
  4. set index 0
  5. set result {}
  6. while {[regexp -start $index {\{\{|\}\}|[^{}]+|\{|\}} $text match]} {
  7. puts stderr [list index $index match $match depth $depth]
  8. if {$match eq "\}\}"} {
  9. incr depth -1
  10. }
  11. if {$depth == 0} {
  12. append result $match
  13. }
  14. if {$match eq "\{\{"} {
  15. incr depth
  16. }
  17. incr index [string length $match]
  18. }
  19.  
  20. puts $result