offset(n) -> [Integer, Integer] | [nil, nil][permalink][rdoc][edit]-
例n番目の部分文字列のオフセットの配列[start, end]を返します。[ self.begin(n), self.end(n) ]と同じです。
n番目の部分文字列がマッチしていなければ[nil, nil]を返します。- [PARAM]
n: - 部分文字列を指定する数値
- [EXCEPTION]
IndexError: -
範囲外の
nを指定した場合に発生します。
[SEE_ALSO] MatchData#begin, MatchData#end, MatchData#byteoffset
- [PARAM]
offset(name) -> [Integer, Integer] | [nil, nil][permalink][rdoc][edit]-
例nameという名前付きグループに対応する部分文字列のオフセットの配列[start, end]を返します。[ self.begin(name), self.end(name) ]と同じです。
nameの名前付きグループにマッチした部分文字列がなければ[nil, nil]を返します。- [PARAM]
name: - 名前(シンボルか文字列)
- [EXCEPTION]
IndexError: -
正規表現中で定義されていない
nameを指定した場合に発生します。
/(?<year>\d{4})年(?<month>\d{1,2})月(?:(?<day>\d{1,2})日)?/ =~ "2021年1月" p $~.offset('year') # => [0, 4] p $~.offset(:year) # => [0, 4] p $~.offset('month') # => [5, 6] p $~.offset(:month) # => [5, 6] p $~.offset('day') # => [nil, nil] p $~.offset('century') # ~> IndexError: undefined group name reference: century[SEE_ALSO] MatchData#begin, MatchData#end, MatchData#byteoffset
- [PARAM]