From: Brian Candler Date: 2010-09-18T04:26:38+09:00 Subject: Re: search and replace John Smith wrote: > I want to be able to run a program that will do a bunch > of searches specified in an array of strings, and replace the > first occurrence of those strings with strings specified on a > second array for a given text file. > > What would be the best way to do this? Have you got something simple working yet? For example, you could write a loop which matches each of your test strings in turn, and replaces with the corresponding target string. >> str = "hello" => "hello" >> str["el"] = "a" => "a" >> str => "halo" Once you've got that working, then you can try something more sophisticated if your application demands it. One way would be to build a Regexp (programmatically) which matches any of your test strings, and use gsub with a block to perform the replacements. But it's a bit trickier there to only replace the *first* occurrence of each. -- Posted via http://www.ruby-forum.com/.