Ruby 2.3.0 リファレンスマニュアル > ライブラリ一覧 > strscanライブラリ > StringScannerクラス > []

instance method StringScanner#[]

self[nth] -> String | nil[permalink][rdoc]

前回マッチした正規表現の nth 番目のかっこに対応する部分文字列を返します。インデックス 0 はマッチした部分全体です。前回のマッチが失敗していると常に nil を返します。

[PARAM] nth:
前回マッチした正規表現の nth 番目のかっこに対応する部分文字列を返します。


require 'strscan'

s = StringScanner.new('test string')
s.scan(/\w(\w)(\w*)/) # => "test"
s[0]                  # => "test"
s[1]                  # => "e"
s[2]                  # => "st"
s.scan(/\w+/)         # => nil
s[0]                  # => nil
s[1]                  # => nil
s[2]                  # => nil
s.scan(/\s+/)         # => " "
s[0]                  # => " "
s[1]                  # => nil
s[2]                  # => nil
s.scan(/\w(\w)(\w*)/) # => "string"
s[0]                  # => "string"
s[1]                  # => "t"
s[2]                  # => "ring"