hmmm, regexes don't really work well in this situation. what you could do is the technique lisp programmers use to make sure their parentheses all match up: you start at zero, then add one for every '(' and subtract one for every ')', and you do this linearly, that is, start at the beginning and assign to each '(' or ')' the number that results from the addition or subtraction. then, to find the set of matching parens, you just find the first occurance of two equal numbers. this shouldn't be too hard:[tt]
my $text = "junk{hi{how{are{you}}}}junk";
my $bc = 0;
my @ba;
while ($text =~ /(\{|\}).*?(?=(\{|\}))/g)
{
if ($1 eq '{') {$bc += 1} else {$bc -= 1}
last if ($bc <= 0);
@ba[1..$bc] = map {$_ . $&} @ba[1..$bc];
}
@ba = map {$_ . '}'} @ba[1..$#ba];
print join("\n", @ba);[/tt]
this may not be the absolute best way to do it, both in efficiency and logicality, but it will do what you're looking for. @ba will be an array where $ba[1] will be the data and brackets that occur first, $ba[2] will be the second set, &c.
if this were cleaned up a bit, it would make a nice addition to the perl cookbook.
stillflame out. "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."