Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl Regex substitution: replace nth occurrance

Reply
Thread Tools

Perl Regex substitution: replace nth occurrance

 
 
Yogi
Guest
Posts: n/a
 
      04-09-2008
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.
 
Reply With Quote
 
 
 
 
Yogi
Guest
Posts: n/a
 
      04-09-2008
On Apr 9, 12:05 pm, Frank Seitz <devnull4...@web.de> wrote:
> 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


Thanks Frank for your help.
Regards,
-Yogi
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      04-09-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with regex search-and-replace (Perl to Python) Schif Schaf Python 12 02-08-2010 08:30 AM
help with perl search/replace regex solaristar Perl Misc 3 01-22-2010 08:15 PM
Want regex s/// to replace only nth occurrence jerrykrinock@gmail.com Perl Misc 12 07-09-2008 02:04 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Algorithm to find nth largest or nth smallest in a range Code4u C++ 4 07-13-2005 03:18 AM



Advertisments