Tbpgr Blog

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

Ruby | MatchData | pre_match

概要

MatchData#pre_match -> String

詳細

マッチした部分より前の文字列を返却

$`

と同じ。

サンプルコード
require 'tbpgr_utils'

"@@@@@hogehigehage" =~ /(h\wge)(h\wge)(h\wge)/

bulk_puts_eval binding, <<-EOS
$`
$~.pre_match
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

$`           # => "@@@@@"
$~.pre_match # => "@@@@@"