End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method · added in Ruby 3.1
MatchData#match_length
mtch.match_length(n) → array
re.c
View on GitHub ↗
static VALUE
match_nth_length(VALUE match, VALUE n)
{
int i = match_backref_number(match, n);
struct re_registers *regs = RMATCH_REGS(match);
match_check(match);
backref_number_check(regs, i);
if (BEG(i) < 0)
return Qnil;
update_char_offset(match);
const struct rmatch_offset *const ofs =
&RMATCH(match)->rmatch->char_offset[i];
return LONG2NUM(ofs->end - ofs->beg);
}
Returns the length of the captured substring corresponding to the argument. n can be a string or symbol to reference a named capture.
Example
m = /(.)(.)(\d+)(\d)(\w)?/.match("THX1138.") m.match_length(0) #=> 6 m.match_length(4) #=> 1 m.match_length(5) #=> nil m = /(?<foo>.)(.)(?<bar>.+)/.match("hoge") m.match_length(:foo) #=> 1 m.match_length(:bar) #=> 2
Ruby version changes
-
3.1
-
MatchData#match_lengthis added [Feature #18172]
-