Perl Regex substitution: replace nth occurrance

Discussion in 'Perl Misc' started by Yogi, Apr 9, 2008.

  1. Yogi

    Yogi Guest

    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.
    Yogi, Apr 9, 2008
    #1
    1. Advertising

  2. Yogi

    Yogi Guest

    On Apr 9, 12:05 pm, Frank Seitz <> 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
    Yogi, Apr 9, 2008
    #2
    1. Advertising

  3. 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
    John W. Krahn, Apr 9, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Code4u
    Replies:
    4
    Views:
    2,640
    Stephen Howe
    Jul 13, 2005
  2. Replies:
    3
    Views:
    746
    Reedick, Andrew
    Jul 1, 2008
  3. Ray Muforosky

    string substitution help (search and replace)

    Ray Muforosky, Mar 9, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    122
  4. Replies:
    12
    Views:
    448
    Ben Morrow
    Jul 9, 2008
  5. Joan Interactive Bussiness
    Replies:
    2
    Views:
    114
    Kyle T. Jones
    Feb 18, 2010
Loading...

Share This Page