Click here to get back home

Perl Regex substitution: replace nth occurrance

 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
Perl Regex substitution: replace nth occurrance Yogi 04-09-2008
Posted by Yogi on April 9, 2008, 2:44 am
Please log in for more thread options
Hi Guys,
I have a variable say:
$x = "this is test program with test inputs";

My requirement is to replace nth occurrance of "test" with something
else. how to achieve the same using perl regex. if i do something
like:
$x =~ s/test/java/g;

This is going to replace all occurrance of test with "java" but my
requirement is to replace say 2nd occurrance only. Any help?

Regards.

Posted by Frank Seitz on April 9, 2008, 3:05 am
Please log in for more thread options
Yogi wrote:
> I have a variable say:
> $x = "this is test program with test inputs";
>
> My requirement is to replace nth occurrance of "test" with something
> else. how to achieve the same using perl regex. if i do something
> like:
> $x =~ s/test/java/g;
>
> This is going to replace all occurrance of test with "java" but my
> requirement is to replace say 2nd occurrance only. Any help?

perldoc -q nth

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Posted by Yogi on April 9, 2008, 4:12 am
Please log in for more thread options
> Yogi wrote:
> > I have a variable say:
> > $x =3D "this is test program with test inputs";
>
> > My requirement is to replace nth occurrance of "test" with something
> > else. how to achieve the same using perl regex. if i do something
> > like:
> > $x =3D~ s/test/java/g;
>
> > This is going to replace all occurrance of test with "java" but my
> > requirement is to replace say 2nd occurrance only. Any help?
>
> perldoc -q nth
>
> Frank
> --
> Dipl.-Inform. Frank Seitz;http://www.fseitz.de/
> Anwendungen f=FCr Ihr Internet und Intranet
> Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Thanks Frank for your help. :)
Regards,
-Yogi

Posted by John W. Krahn on April 9, 2008, 2:08 pm
Please log in for more thread options
Yogi wrote:
> Hi Guys,
> I have a variable say:
> $x = "this is test program with test inputs";
>
> My requirement is to replace nth occurrance of "test" with something
> else. how to achieve the same using perl regex. if i do something
> like:
> $x =~ s/test/java/g;
>
> This is going to replace all occurrance of test with "java" but my
> requirement is to replace say 2nd occurrance only. Any help?

$ perl -le'
my $test = q[ 1 test 2 test 3 test 4 test 5 test 6 test 7 test ];
print $test;
my $count;
my $to_java = 4;
while ( $test =~ /test/g ) {
if ( $to_java == ++$count ) {
substr $test, $-[0], $+[0] - $-[0], q[java];
}
}
print $test;
'
1 test 2 test 3 test 4 test 5 test 6 test 7 test
1 test 2 test 3 test 4 java 5 test 6 test 7 test



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
string substitution help (search and replace) March 9, 2005, 1:39 pm
Substitution (regex?) August 26, 2004, 2:51 pm
Re: Need help with a Regex substitution March 23, 2007, 5:23 pm
Re: Need help with a Regex substitution March 24, 2007, 9:41 am
regex substitution problem March 6, 2006, 1:23 pm
Regex: All except leading global substitution... March 9, 2006, 11:51 pm
How to make a regex substitution repeat until there are no more matches? February 14, 2005, 4:11 am
regex search and replace July 30, 2005, 7:07 am
Want regex s/// to replace only nth occurrence July 6, 2008, 9:17 am
Regex, how do I replace quotation pairs into
  • &
  • ?
    October 21, 2004, 7:27 pm

    Our other projects:

    Art Dolls, Fairies and Mermaids - Sunnyfaces.net

    Roy's Linux, Programming and Search Engines messages

    1-Script XML SitemapXML Sitemap