Substitution problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rs1975in
    New Member
    • Oct 2006
    • 1

    Substitution problem

    I need one help regarding regular expressions in PERL

    I have a string like

    test[01,02,03],tesst11*,rohit[01-20]

    I need to write a regular expression for substitution which will make
    this string like

    test[01,02,03] tesst11* rohit[01-20]

    that means I want to substitute only those commas by space which are
    not in square brackets. Any quick idea..?

    regards
    Sharma
  • sstouk
    New Member
    • Oct 2006
    • 3

    #2
    my($Str1) = 'test[01,02,03],tesst11*,rohit[01-20]';
    my($Str2) = undef;
    my(@array1) = undef;
    my(@array2) = undef;

    while ($Str1 =~ m!\[(.*?)\]!gis){push @array1,$1};
    @array2 = split(/\[.*?\]/gis,$Str1);

    for ($i = 0; $i < @array2; $i++)
    {
    $array2[$i] =~ s!,! !gis;
    $Str2 .= $array2[$i]."[".$array1[$i+1]."]";
    };

    print "$Str2\n";

    Comment

    Working...