From: Simon Baird Date: 2006-07-28T13:19:00+09:00 Subject: Re: Equivalent of perl's \Q in regexps? ------=_Part_279267_5897321.1154060336380 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 7/28/06, Logan Capaldo wrote: > > > On Jul 27, 2006, at 9:10 PM, Simon Baird wrote: > > > marker = '***' > > > > code = <<-EOT > > var x = 1 > > //*** hello > > function foo() {} > > EOT > > > > p code.to_a.grep(/\*\*\*/) # -> ["//*** hello\n"] that's > > what I > > want but using marker > > > > #p code.to_a.grep(/#{marker}/) # -> invalid regexp error > > > > p code.to_a.grep(/#{%q(marker)}/) # -> [], doesn't work > > > > > > > > In perl you can do /\Q$marker/ and it escapes $marker for the regexp. > > > > > > Just guessing but I believe you want > /#{Regexp.escape(marker)}/ > > or Regexp.new(Regexp.escape(marker)) Cool. It works. Thanks Not quite as pretty as I'd like. Maybe a %R(...) would be a nice sweetener. Simon. -- Simon Baird ------=_Part_279267_5897321.1154060336380--