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.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 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:
But that's not exactly what was asked for originally. It just happens to be good for this problem and this input data.s/\d+_// for 1..2;
Well, it's what was literally asked for, but it's not very generalizable. {grin}
-- Randal L. Schwartz, Perl hacker
In reply to Re: (tye)Re: changing only the first two matches of a regex
by merlyn
in thread changing only the first two matches of a regex
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |