Using Ruby 1.8.7 I want to store some regular expressions in the database and have them be easily reconstituted as a Regexp
object when I need to use them for validation. I've found that Ruby exhibits some unwanted behavior. For instance:
r = Regexp.new(/^\w+$/i) => /^\w+$/i
r_i = r.inspect => "/^\\w+$/i"
r_s = r.to_s => "(?i-mx:^\\w+$)"
r_from_r_i = Regexp.new(r_i) => /\/^\w+$\/i/
r_from_r_s = Regexp.new(r_s) => /(?i-mx:^\w+$)/
r == r_from_r_i => false
r == r_from_r_s => false
What I'd like is to be able to store the Regexp
in the database in the form /pattern/options
instead of the (?options:pattern)
format, because I'm more familiar with the former. But, when creating a new Regexp
from a variable, the pattern gets all wacky. Ruby is escaping the /
s - so the pattern is invalid.
Is there any way to make Ruby honor the /pattern/options
format when parsing a regular expression?