in reply to Regex required to Replace the array specifiers in a string

I wouldn't try to do this using regular expressions but would use a state engine instead.

use strict; use warnings; use 5.010; my $string = q{TestVar(Test1->(xy))->Var2(Test2(10)(12))->Finalvar}; say $string; my $parenDepth = 0; my $newString = q{}; foreach my $char ( split m{}, $string ) { if ( $char eq q{(} ) { $parenDepth ++; } elsif ( $char eq q{)} ) { $parenDepth --; } else { $newString .= $char unless $parenDepth; } } say $newString;

The output

TestVar(Test1->(xy))->Var2(Test2(10)(12))->Finalvar TestVar->Var2->Finalvar

I hope this is helpful.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Regex required to Replace the array specifiers in a string
by anandvn (Novice) on Jul 02, 2010 at 14:34 UTC
    Hi John,

    Thanks a lot for the prompt response. This logic is working fine. But i face perfomance issue since my input sample is huge (app 1 lakh strings). Hence i would be grateful if this can be solved using regex or any other efficient way.

    Cheers,
    Anand

      If you think you are having performance issues, profile your code to check where your bottlenecks are. Devel::NYTProf is an example of a good utility in this area. We cannot help you improve performance unless you provide specific pieces of code that you have experimentally demonstrated are your bottlenecks.