Alex Rivera | Logout

Interpolating a string into a regex

Asked 2008-09-29T18:48:33.673
181

I need to substitute the value of a string into my regular expression in Ruby. Is there an easy way to do this? For example:

foo = "0.0.0.0"
goo = "here is some other stuff 0.0.0.0" 
if goo =~ /value of foo here dynamically/
  puts "success!"
end
Edit
Report

1 Answer

6
Regexp.compile(Regexp.escape(foo))
answered 2008-09-29T18:53:16.987

Your Answer