in reply to Selective substitution: not in Perl?
a quick hack to solve your problem:
$n holds the number of occurences to be skipped, so $n = 1 means the second will be replaced. The output:#!/usr/bin/perl -w use strict; my $string = "foo" x 4; print "$string\n"; my $pattern = "foo"; my $better = "bar"; my $n=1; $string =~ s/^(($pattern){$n})$pattern/$1$better/; print "$string\n";
I believe that's what you wanted. IMHO it ain't ugly, too. ;-)atl@companion:~/perl/s > one.pl foofoofoofoo foobarfoofoo
Have fun ...
Andreas
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: RE: Selective substitution: not in Perl?
by nuance (Hermit) on Aug 16, 2000 at 14:39 UTC | |
by atl (Pilgrim) on Aug 16, 2000 at 15:27 UTC | |
by merlyn (Sage) on Aug 16, 2000 at 15:33 UTC | |
by Anonymous Monk on Aug 17, 2000 at 01:46 UTC | |
|
RE: RE: Selective substitution: not in Perl?
by davorg (Chancellor) on Aug 16, 2000 at 14:37 UTC | |
by nuance (Hermit) on Aug 16, 2000 at 14:49 UTC | |
by davorg (Chancellor) on Aug 16, 2000 at 14:51 UTC |