From: Robert Klemme Date: 2003-11-25T01:32:10+09:00 Subject: Re: using variables in regular expressions "Damphyr" schrieb im Newsbeitrag news:3FC2104E.7080607@freemail.gr... > Being lazy, forgetful and generally very bad with regular expressions > leads me to post this question: > > How can I use the contents of a variable as a pattern in a regular > expression? > > I get a list of files from a set of paths and I want to do the following > > repo=list_files paths > repo.collect!{|entry| > entry.gsub(/{variable_with_the root_I_want_to_substitute}/,"") > } using gsub! is more efficient and better use %r{} because Regexp.quote does not quote "/": repo=list_files paths repo.each {|entry| entry.gsub!(%r{#{Regexp.quote(variable_with_the root_I_want_to_substitute)}},"") } Regards robert