Sep 27, 2010 #1 jr8rdt IS-IT--Management Joined Feb 9, 2006 Messages 59 Location US Hi , how can I search and replace the second (or n) occurrence ? this will replace ALL $b with $c $a =~ s/$b/$c/g; I just want to replace the 2nd $b with $c Thanks
Hi , how can I search and replace the second (or n) occurrence ? this will replace ALL $b with $c $a =~ s/$b/$c/g; I just want to replace the 2nd $b with $c Thanks
Sep 27, 2010 #2 PinkeyNBrain IS-IT--Management Joined Dec 12, 2006 Messages 279 Location US This may not be the most elegant, but should get you pretty close Code: $str = 'abc123abc456' ; $b = 'abc'; $c = 'xyz' ; $str =~ s/(.*?$b)(.*?)$b(.*)/$1$2$c$3/ ; Upvote 0 Downvote
This may not be the most elegant, but should get you pretty close Code: $str = 'abc123abc456' ; $b = 'abc'; $c = 'xyz' ; $str =~ s/(.*?$b)(.*?)$b(.*)/$1$2$c$3/ ;
Sep 28, 2010 #3 prex1 Programmer Joined May 10, 2007 Messages 482 Location IT A bit simpler Code: $str=~s/$b(.*?)$b/$b$1$c/; Franco http://www.xcalcs.com : Online engineering calculations http://www.megamag.it : Magnetic brakes for fun rides http://www.levitans.com : Air bearing pads Upvote 0 Downvote
A bit simpler Code: $str=~s/$b(.*?)$b/$b$1$c/; Franco http://www.xcalcs.com : Online engineering calculations http://www.megamag.it : Magnetic brakes for fun rides http://www.levitans.com : Air bearing pads
Sep 28, 2010 #4 PinkeyNBrain IS-IT--Management Joined Dec 12, 2006 Messages 279 Location US Simpler and faster by 17% to 20% (longer strings will get a better % rate). Good call. Upvote 0 Downvote
Sep 28, 2010 Thread starter #5 jr8rdt IS-IT--Management Joined Feb 9, 2006 Messages 59 Location US it works!! Thanks Upvote 0 Downvote