in reply to changing only the first two matches of a regex

Here is a fun (and powerful) way to only do some substitutions:

my $str= "1_abc/2_def/bla_30_31_blah"; my $count= 0; while( $str =~ /\d+_/g ) { last if 2 < ++$count; $str =~ s///; } print "\$str=($str)\n"; # $str=(abc/def/bla_30_31_blah)
Though I'd probably use one of the other solutions mentioned in this particular case.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: changing only the first two matches of a regex
by merlyn (Sage) on Mar 20, 2001 at 23:30 UTC
    my $str= "1_abc/2_def/bla_30_31_blah"; my $count= 0; while( $str =~ /\d+_/g ) { last if 2 < ++$count; $str =~ s///; } print "\$str=($str)\n";
    I believe that middle s/// is not doing what you think it is. It's going back to the beginning of the string, looking for the first match that matches the most recent successful regex match, and then removing it.

    I think you lucked out on this one. Had your replacement been s//49_/, you would have seen only the first one changed, since the first replacement string would have been matched again on the second while-pass.

    Oh, and even more so... I don't think your //g in the while is actually inching along! I think it's scanning from the beginning again as well, since you modify the scalar within the loop. So you just did this the hard way:

    s/\d+_// for 1..2;
    But that's not exactly what was asked for originally. It just happens to be good for this problem and this input data.

    Well, it's what was literally asked for, but it's not very generalizable. {grin}

    -- Randal L. Schwartz, Perl hacker

      Thanks. I was surprised when that appeared worked.

      Well, it would have been powerful... (:

              - tye (but my friends call me "Tye")