From: Intransition Date: 2009-12-08T14:10:23+09:00 Subject: Re: Regexp.escape with un-escapes On Dec 6, 12:45 pm, Brian Candler wrote: > There is a feature in String#split (documented in 1.9, undocumented in > 1.8.6 but still present) whereby if the split RE contains capture > groups, those capture groups will be included in the resulting array. > > >> s = 'here is (a) setting \'a\' equal to ((\d+))' > > => "here is (a) setting 'a' equal to ((\\d+))">> Regexp.new(s.split(/(\(\(.*?\)\))/).map { |x| x =~ /\A\((\(.*\))\)\z/ ? $1 : Regexp.escape(x) }.join) > > => /here\ is\ \(a\)\ setting\ 'a'\ equal\ to\ (\d+)/ Weird, but cool. Does the trick. Thanks.