Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method · added in Ruby 3.2

MatchData#byteoffset

mtch.byteoffset(n) → array

Returns a two-element array containing the beginning and ending byte-based offsets of the nth match. n can be a string or symbol to reference a named capture.

Example
m = /(.)(.)(\d+)(\d)/.match("THX1138.")
m.byteoffset(0)      #=> [1, 7]
m.byteoffset(4)      #=> [6, 7]

m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
p m.byteoffset(:foo) #=> [0, 1]
p m.byteoffset(:bar) #=> [2, 3]

Ruby version changes

  1. 3.2
See also #begin #captures #[] #deconstruct also in 4.1 · 4.0 · 3.4 · 3.2