From: Rob Biedenharn Date: 2010-02-23T04:22:26+09:00 Subject: Re: How do I use a variable in a regexp search string? On Feb 22, 2010, at 1:45 PM, joemac wrote: > On Jan 29, 7:38 pm, Harry Kakueki wrote: >>> I dont know how to use avariablein a regex searchstring. >>> ruby seems to only handle hardcoded serachstring in >>> regex's. How do you do this. My code is below. >> >> Does this help? >> >> str = "abcdef" >> searchstring = "cd" >> p str =~ /#{searchstring}/ >> >> Harry > > Hi Folks, > > Thanks for all the helpful info. > It was the #{} syntax that i was missing. > This also works for the "filename as a varible" situation > when parsing lots of files: > > filenames = ["input1", "input2", "input3"] > > for fname in filenames > infile = File.open("#{fname}") > while line = infile.gets() > puts line > end > puts "================== end file ===================" > end > > > Very helpfull! > > Thanks, --JM In this case, you're already getting strings in fname so "#{fname}" isn't really doing anything useful. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com