tags:

views:

74

answers:

1

I have a problem with a regular expression. I am trying to extract the multiple conditions inside while and if. I am using Perl 5.8.6 on Windows XP.

@conditions is an array that exactly contains the while loop contents, e.g.:

while
 (
    (condition A) &&
    (condition B)
 )  

You can assume @conditions array contains the above data:

my perl code sampled:

my $count = 0; 
foreach my $condition (@conditions) { 
    $count++; 
    my ($open,$close) = $condition =~ /( (?: [(] | \s )* ) (.*) /msx; 
    print "$open $count $close"; 
    } 

and my C code:

 while
 (
   ( 
    1 condition A &&
    2 condition B
   )&&
   (
    3 condition C
    4 condition D
   )
 )

I am facing the problem when I have a while loop like this:

while
 (

     (condition A) &&
     (condition B)
 )  

My desired output has to be:

 while
 (

    1 (condition A) &&
    2 (condition B)
 ) 

but it prints has...

while
(
   ( 1 condition A) &&
   ( 2 condition B)

)

Can anyone help me with the regex to get my desired output?

+2  A: 

You have print "$open $count $close"; and you state that you want the number to preceed the parenthesis.

So use print "$count $open$close";

Anonymous
lokesh
My code prints a space between the numeral and the opening parenthesis. Show what code you're actually using, because it's not the same.
Anonymous