Tbpgr Blog

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

Ruby | Regexp | ~

概要

Regexp#-

詳細

以下と同じ

self =~ $_
サンプルコード
require 'tbpgr_utils'

$_ = "hogehigehage"

~ /(h.ge)(h.ge)(h.ge)/
bulk_puts_eval binding, <<-EOS
Regexp.last_match
Regexp.last_match(0)
Regexp.last_match(1)
Regexp.last_match(2)
Regexp.last_match(3)
Regexp.last_match(4)
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

Regexp.last_match    # =>                #<MatchData "hogehigehage" 1:"hoge" 2:"hige" 3:"hage">
Regexp.last_match(0) # => "hogehigehage"
Regexp.last_match(1) # => "hoge"
Regexp.last_match(2) # => "hige"
Regexp.last_match(3) # => "hage"
Regexp.last_match(4) # => nil