Ruby 3.2 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Stringクラス > byterindex
byterindex(pattern, offset = self.bytesize) -> Integer | nil
[permalink][rdoc]文字列のバイト単位のインデックス offset から左に向かって pattern を探索します。最初に見つかった部分文字列の左端のバイト単位のインデックスを返します。見つからなければ nil を返します。
引数 pattern は探索する部分文字列または正規表現で指定します。
offset が負の場合は、文字列の末尾から数えた位置から探索します。
byterindex と String#byteindex とでは、探索方向だけが逆になります。完全に左右が反転した動作をするわけではありません。探索はその開始位置を右から左にずらしながら行いますが、部分文字列の照合はどちらのメソッドも左から右に向かって行います。以下の例を参照してください。
p "stringstring".byteindex("ing", 1) # => 3
# ing # ここから探索を始める
# ing
# ing # 右にずらしていってここで見つかる
p "stringstring".byterindex("ing", -1) # => 9
# ing # インデックス -1 の文字から探索を始める
# ing
# ing # 左にずらしていってここで見つかる
'foo'.byterindex('f') # => 0
'foo'.byterindex('o') # => 2
'foo'.byterindex('oo') # => 1
'foo'.byterindex('ooo') # => nil
'foo'.byterindex(/f/) # => 0
'foo'.byterindex(/o/) # => 2
'foo'.byterindex(/oo/) # => 1
'foo'.byterindex(/ooo/) # => nil
# 右でのマッチが優先
'foo'.byterindex(/o+/) # => 2
$~ #=> #<MatchData "o">
# 最長にするには否定戻り読み(negative look-behind)と組み合わせる
'foo'.byterindex(/(?<!o)o+/) # => 1
$~ #=> #<MatchData "oo">
# またはbyteindexを否定先読み(negative look-ahead)
'foo'.byteindex(/o+(?!.*o)/) # => 1
$~ #=> #<MatchData "oo">
'foo'.byterindex('o', 0) # => nil
'foo'.byterindex('o', 1) # => 1
'foo'.byterindex('o', 2) # => 2
'foo'.byterindex('o', 3) # => 2
'foo'.byterindex('o', -1) # => 2
'foo'.byterindex('o', -2) # => 1
'foo'.byterindex('o', -3) # => nil
'foo'.byterindex('o', -4) # => nil
[SEE_ALSO] String#rindex, String#byteindex