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.
In reply to Re: Braces
by chipmunk
in thread Braces
by sangam1
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.