Click here to get back home

Want regex s/// to replace only nth occurrence

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Want regex s/// to replace only nth occurrence jerrykrinock 07-06-2008
Get Chitika Premium
Posted by jerrykrinock on July 6, 2008, 9:17 am
Please log in for more thread options
my $s = "The sneaky cat sneaked sneakily." ;

I would like a simple s/// statement which would replace only the $nth
occurrence of "sneak". ($n is a variable). Can't find the answer.
Is this possible?

Thanks,

Jerry Krinock

Posted by Gunnar Hjalmarsson on July 6, 2008, 9:46 am
Please log in for more thread options
jerrykrinock@gmail.com wrote:
> my $s = "The sneaky cat sneaked sneakily." ;
>
> I would like a simple s/// statement which would replace only the $nth
> occurrence of "sneak". ($n is a variable).

my $repl = 'xyz';
my $n = 2;
my $i;
$s =~ s/(sneak)/ ++$i == $n ? $repl : $1 /eg;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by Dave B on July 6, 2008, 9:58 am
Please log in for more thread options
jerrykrinock@gmail.com wrote:

> my $s = "The sneaky cat sneaked sneakily." ;
>
> I would like a simple s/// statement which would replace only the $nth
> occurrence of "sneak". ($n is a variable). Can't find the answer.
> Is this possible?

With sed it's trivial. With Perl, I'd do this:

my $x=0;my $n=3;

while($s=~/sneak/g) {
if (++$x==$n) {
$s=~s/sneak\G/replacement/;
last;
}
}

print "$s\n";

Outputs:

The sneaky cat sneaked replacementily.

I'm a Perl beginner, so there are more idiomatic ways of expressing that, I
guess.

--
D.

Posted by comp.llang.perl.moderated on July 8, 2008, 8:11 am
Please log in for more thread options
> jerrykrin...@gmail.com wrote:
> > my $s = "The sneaky cat sneaked sneakily." ;
>
> > I would like a simple s/// statement which would replace only the $nth
> > occurrence of "sneak". ($n is a variable). Can't find the answer.
> > Is this possible?
>
> With sed it's trivial. With Perl, I'd do this:
>
> my $x=0;my $n=3;
>
> while($s=~/sneak/g) {
> if (++$x==$n) {
> $s=~s/sneak\G/replacement/;

alternatively:

substr($s, $-[0], $+[0]-$-[0]) = replacement;

> last;
> }
>
> }
>
--
Charles DeRykus

Posted by John W. Krahn on July 8, 2008, 12:42 pm
Please log in for more thread options
comp.llang.perl.moderated wrote:
>> jerrykrin...@gmail.com wrote:
>>> my $s = "The sneaky cat sneaked sneakily." ;
>>> I would like a simple s/// statement which would replace only the $nth
>>> occurrence of "sneak". ($n is a variable). Can't find the answer.
>>> Is this possible?
>> With sed it's trivial. With Perl, I'd do this:
>>
>> my $x=0;my $n=3;
>>
>> while($s=~/sneak/g) {
>> if (++$x==$n) {
>> $s=~s/sneak\G/replacement/;
>
> alternatively:
>
> substr($s, $-[0], $+[0]-$-[0]) = replacement;

alternatively:

substr $s, $-[0], $+[0] - $-[0], 'replacement';

>> last;
>> }
>>
>> }


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Similar ThreadsPosted
regex search and replace July 30, 2005, 7:07 am
Regex, how do I replace quotation pairs into
  • &
  • ?
    October 21, 2004, 7:27 pm
    RegEx replace "bracketed text" April 16, 2006, 1:36 pm
    regex replace credit card numbers with * September 29, 2005, 6:41 am
    Regex failed to replace utf8 character November 29, 2006, 12:25 pm
    Perl Regex substitution: replace nth occurrance April 9, 2008, 2:44 am
    Passing in the replace string for a regex via the command line -HOW? March 3, 2005, 12:47 pm
    regex multi-line match/replace issue April 24, 2006, 4:18 pm
    FAQ 4.28: How do I change the Nth occurrence of something? November 8, 2004, 12:03 pm
    FAQ 4.28 How do I change the Nth occurrence of something? January 21, 2005, 6:03 am

    Our other projects:

    Art Dolls, Fairies and Mermaids - Sunnyfaces.net

    Roy's Linux, Programming and Search Engines messages

    1-Script XML SitemapXML Sitemap