Tbpgr Blog

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

Ruby | MatchData | end

概要

MatchData#end(n) -> Fixnum | nil

詳細

n 番目の部分文字列終端のオフセットを返します。
0 はマッチ全体を意味します。
n 番目の部分文字列がマッチしていなければ nil を返します。

サンプルコード
require 'tbpgr_utils'

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

bulk_puts_eval binding, <<-EOS
$~.size
$~.end 0
$~.end 1
$~.end 2
$~.end 3
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

$~.size  # => 4
$~.end 0 # => 12
$~.end 1 # => 4
$~.end 2 # => 8
$~.end 3 # => 12