Actually, flipping through the Effective Perl Programming book, I see that there is an example of nested braces.
 
snip from book:
$_ = " Here are { nested {} {braces} }!";
{
 my $c;
  while(/([{}])/gc) {
     last unless ($c += {qw({ 1 } -1)}->{$1}) > 0
  };
}
print substr substr($_, 0, pos()), index($_, "{");
 
End snip;
 
might be more Perlish way of tackling your problem
 
Joe.
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Joseph Youngquist
Sent: Friday, July 05, 2002 9:07 AM
To: 'Dhiraj Kapoor'; [EMAIL PROTECTED]
Subject: RE: Extracing bracket matching texts

$string = "( A = (B= (D = 1) (P =1 )) ( F = (Y = 2 ) ( w = 3)) (B = (D = 3) ( P = 4)) )";
 
while($string =~ /\(\s*(\w+\s*\=\s*(?:\(\s*\w+\s*\=\s*\d+\s*\)\s*)+)\)/ig)
 {
     print "\n$1"; # just to print them. or
     push @matches, $1;  # push the matched into an array for later use
 }
print "\n\n:::Here's what I found:::\n";
print join "\n", @matches;
 
You can add in the regular expression the test for B (change first '\w+' to 'B') or keep it general and test the @matches for strings beginning with 'B'
 
hth,
Joe
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Dhiraj Kapoor
Sent: Friday, July 05, 2002 8:52 AM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: Extracing bracket matching texts

Hi,
    I need to extract bracket matching text from text full of opening and closing brackets like
    ( A = (B= (D = 1) (P =1 )) ( F = (Y = 2 ) ( w = 3)) (B = (D = 3) ( P = 4)) )
        I need to extract text starting from (B =  or ( F =  or all texts starting with (B =  like to the text for "(B ="  will be "B= (D = 1) (P =1 )" and  "B = (D = 3) ( P = 4)".
 
     Basically, to get texts starting with open paranthesis and its closing parenthesis knowing the first word of the starting parenthesis
   
thanks
Dhiraj

Reply via email to