Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by ikegami (Pope)
on Jul 14, 2005 at 22:48 UTC ( #475077=note: print w/replies, xml ) Need Help??


in reply to Re^2: Replacing a specfied instance of a pattern in a string
in thread Replacing a specfied instance of a pattern in a string

That reminds me of /c!
my $str = 'Fsih my test variable is fsihd or is it gfsih or gfsih +d fsih'; # 1111 2222 3333 + 4444 my $substr = 'fsih'; my $regexp = qr/\Q$substr\E/; my $replace = 'fish'; my $instance = 3; # 3rd print("$str\n"); scalar $str =~ /\G.*?$regexp/gc while --$instance; $str =~ s/\G(.*?)$regexp/$1$replace/; print("$str\n");

The /c is needed in case there are less than $instance instances of the search string.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://475077]
help
Chatterbox?
[stevieb]: I literally laughed. That's good :) Perhaps I just need to go climb another mountain and forget about it
shmem seeks a tate and smikes a pope
shmem .oO( Ca n'est pas un pipe )
[shmem]: ah well. goodn.(1<<3) folks

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2017-03-28 22:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (342 votes). Check out past polls.