Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Answer: How do i find $a and replace it with $b when it's preceded by $c?

by nuance (Hermit)
on Jun 13, 2000 at 13:01 UTC ( #17850=categorized answer: print w/replies, xml ) Need Help??

Q&A > regular expressions > How do i find $a and replace it with $b when it's preceded by $c? - Answer contributed by nuance

A postivive look behind clause in the regular expression will do what you want.
my($a, $b, $c, $d) = qw{foo bar moo moofooyou); $d =~ s/(?<=$c)$a/$b/g;
produces moobaryou in $d

  • Comment on Answer: How do i find $a and replace it with $b when it's preceded by $c?
  • Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2016-10-01 11:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:







    Results (576 votes). Check out past polls.