in reply to Braces

Matching nested balanced delimiters with regular expressions is a hard thing to do. Actually, with true regular expressions, it is impossible. Even when backreferences are added, it's still impossible.

However, perl5.6 adds experimental features to the regular expression syntax which actually make it possible to match nested delimiters with a single (but complicated :) regular expression. I used the example from perlre from perl5.6 for matching balanced parentheses and came up with this code:

#!/usr/local/bin/perl require 5.006; my $braces; $braces = qr/(\{(?:(?>[^\{\}]+)|(??{$braces}))*\})/; sub extract_braces { my($str, $level) = @_; while ($str =~ m!$braces!g) { my $inside = $1; print "\t" x $level, $inside, "\n"; $inside =~ s/\{(.*)\}/$1/; extract_braces($inside, $level + 1); } } my $str = '0a { 1a { 1.1a } 1b { 1.2a } 1c { 1.3a { 1.3.1a } 1.3b } + } 0b { 2a { 2.1a} } 0c'; extract_braces($str, 0); __END__
The experimental regex features used here are (??{}) for postponed evaluation, and (?>) for a non-backtracking match.