Easy way to escape text for use in regexps?

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).

Is there a library somewhere that has this function available?

Thanks,
Wes

Sorry all.

I’ll look harder next time.

How embarrassing.

Regexp.escape()

Thanks,
Wes

Wes G. 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).

Is there a library somewhere that has this function available?

Thanks,
Wes

On May 2, 2006, at 8:45 PM, Wes G. 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('\*?{}.')   #=> \\*\?\{\}\.