in reply to Re (using assertions): pattern matching a limited number of times
in thread pattern matching a limited number of times
Update: Doh! I didn't see your very similar solution before I posted....#!/usr/bin/perl -wT use strict; my $string = 'abc ' x 10; my $search = 'ab'; my $replace = 'CD'; print "$string\n"; my $i = 0; while ($string =~ /$search/gi) { substr($string, $-[0], $+[0]-$-[0], $replace); last if ++$i == 4; pos($string) = $-[0] + 1; } print "$string\n"; __END__ abc abc abc abc abc abc abc abc abc abc CDc CDc CDc CDc abc abc abc abc abc abc
-Blake
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re2: pattern matching a limited number of times
by bart (Canon) on Sep 14, 2002 at 10:14 UTC | |
by BrowserUk (Patriarch) on Sep 14, 2002 at 10:48 UTC |