From: Karl-Heinz Wild Date: 2008-02-06T16:34:23+09:00 Subject: Re: Regexp matching in a block of code On 06.02.2008, at 08:26, Karl-Heinz Wild wrote: > On 06.02.2008, at 04:04, Vell wrote: > >> addresses = ["foo1@bar1.com", "foo2@bar2.com", "foo3@bar3.com"] >> competitors = ["bar.com", "bar1.com", "bar4.com"] >> >> addresses.each do |add| >> competitors.each do |comp| >> puts "Checking Competitor: #{comp}" >> if(add =~ Regexp.escape(comp)) >> puts "#{check} matched" >> else >> puts "No Match" >> end >> end >> end > > > As you can see in the documentation Regexp.escape returns a string > and Regexp.new returns an regexp. > > If you write > > if(add =~ %r { Regexp.escape(comp)) } > > or > > if(add =~ Regexp.new( Regexp.escape(comp)) ) > > you code works for me. You can also write if Regexp.new( comp ).match( add ) ... - Karl-Heinz