Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

MatchData#begin

mtch.begin(n) → integer

Returns the offset of the start of the nth element of the match array in the string. n can be a string or symbol to reference a named capture.

Example
m = /(.)(.)(\d+)(\d)/.match("THX1138.")
m.begin(0)       #=> 1
m.begin(2)       #=> 2

m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
p m.begin(:foo)  #=> 0
p m.begin(:bar)  #=> 2
See also #[] #captures #== #end also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1