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