quoteメソッドは、正規表現に使われるメタ文字をエスケープします。
正規表現に使われるメタ文字の文字列を、そのままの表記の文字列として使いたい場合は、quoteメソッドを使うと便利です。
re = Regexp.new(Regexp.quote("hello* world!"))
p re
p re =~ "hellooooo world!"
p re =~ "hello world!"
p re =~ "hello* world!"
if result = re =~ "hello* world!"
puts "#{result} is true"
else
puts "#{result} is false"
end
実行結果。
/hello\*\ world!/ nil nil 0 0 is true
■この記事のトラックバックURL:
http://www.mapee.jp/mpe334/mt-tb.cgi/380