Tbpgr Blog

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

Ruby | Regexp | to_s

概要

Regexp#to_s -> String

詳細

正規表現の文字列表現を生成して返却。

サンプルコード
require 'tbpgr_utils'

tmp = /(h.ge)(f.o)(b.r)/.to_s
bulk_puts_eval binding, <<-EOS
/h.ge/.to_s
/(h.ge)(f.o)(b.r)/.to_s
/#{tmp}/ =~ 'hogefoobarlast@@@'
$~
/#{tmp}(last)/ =~ 'hogefoobarlast@@@'
$~
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

/h.ge/.to_s                                             # => "(?-mix:h.ge)"
/(h.ge)(f.o)(b.r)/.to_s                                 # => "(?-mix:(h.ge)(f.o)(b.r))"
/(?-mix:(h.ge)(f.o)(b.r))/ =~ 'hogefoobarlast@@@'       # => 0
$~                                                      # =>                            #<MatchData "hogefoobar" 1:"hoge" 2:"foo" 3:"bar">
/(?-mix:(h.ge)(f.o)(b.r))(last)/ =~ 'hogefoobarlast@@@' # => 0
$~                                                      # =>                            #<MatchData "hogefoobarlast" 1:"hoge" 2:"foo" 3:"bar" 4:"last">