End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
MatchData#pre_match
mtch.pre_match → str
re.c
View on GitHub ↗
VALUE
rb_reg_match_pre(VALUE match)
{
VALUE str;
struct re_registers *regs;
if (NIL_P(match)) return Qnil;
match_check(match);
regs = RMATCH_REGS(match);
if (BEG(0) == -1) return Qnil;
str = rb_str_subseq(RMATCH(match)->str, 0, BEG(0));
return str;
}
Returns the portion of the original string before the current match. Equivalent to the special variable $`.
Example
m = /(.)(.)(\d+)(\d)/.match("THX1138.") m.pre_match #=> "T"