Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Replacing a specfied instance of a pattern in a string

by Roy Johnson (Monsignor)
on Jul 14, 2005 at 21:40 UTC ( #475060=note: print w/ replies, xml ) Need Help??


in reply to Replacing a specfied instance of a pattern in a string

One way:

my $foo = 'Fsih my test variable is fsihd or is it gfsih or gfsihd fsi +h'; # Replace the 3rd $foo =~ s/((?:fsih.*?){2})fsih/$1fish/i or warn "Not found"; print "Foo = $foo\n";

Caution: Contents may have been coded under pressure.


Comment on Re: Replacing a specfied instance of a pattern in a string
Download Code
Re^2: Replacing a specfied instance of a pattern in a string
by GrandFather (Sage) on Jul 15, 2005 at 01:08 UTC

    or to pass the index in use:

    use warnings; use strict; my $foo = 'Fsih my test variable is fsihd or is it gfsih or gfsihd fsi +h'; my $i = 2; # Replace the 3rd $foo =~ s/((?:fsih.*?){$i})fsih/$1fish/i or warn "Not found"; print "Foo = $foo\n";

    Perl is Huffman encoded by design.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://475060]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2015-07-04 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls