From: Daniel Harple Date: 2006-05-03T03:50:03+09:00 Subject: Re: Easy way to escape text for use in regexps? On May 2, 2006, at 8:45 PM, Wes Gamble wrote: > I need to be able to include some arbitrary text as input into a > regular > expression and I'm looking for a way to escape regular expression > metacharacters within my data(e.g. escape a "?" for example). $ ri Regexp::escape --------------------------------------------------------- Regexp::escape Regexp.escape(str) => a_str Regexp.quote(str) => a_str ------------------------------------------------------------------------ Escapes any characters that would have special meaning in a regular expression. Returns a new escaped string, or self if no characters are escaped. For any string, Regexp.escape(str)=~str will be true. Regexp.escape('\*?{}.') #=> \\*\?\{\}\.