Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | Regexp | escape

概要

Regexp#escape(string) -> String
Regexp#quote(string) -> String

詳細

正規表現の利用時にエスケープが必要な文字にバックスラッシュを付与する。

サンプルコード
puts Regexp.escape('.*?/+-{}()abc')
puts Regexp.quote('.*?/+-{}()abc')

出力

\.\*\?/\+\-\{\}\(\)abc
\.\*\?/\+\-\{\}\(\)abc