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 | |
by kennethk (Abbot) on Jul 02, 2010 at 15:04 UTC |