[](...)
public
Match Reference—MatchData acts as an array, and may be accessed
using the normal array indexing techniques. mtch[0] is equivalent
to the special variable $&, and returns the entire matched string. mtch[1],
mtch[2], and so on return the values of the matched backreferences
(portions of the pattern between parentheses).
m = /(.)(.)(\d+)(\d)/.match("THX1138.")
m[0]
m[1, 2]
m[1..3]
m[-3, 2]
Show source
/*
* call-seq:
* mtch[i] => obj
* mtch[start, length] => array
* mtch[range] => array
*
* Match Reference---<code>MatchData</code> acts as an array, and may be
* accessed using the normal array indexing techniques. <i>mtch</i>[0] is
* equivalent to the special variable <code>$&</code>, and returns the entire
* matched string. <i>mtch</i>[1], <i>mtch</i>[2], and so on return the values
* of the matched backreferences (portions of the pattern between parentheses).
*
* m = /(.)(.)(\d+)(\d)/.match("THX1138.")
* m[0]
* m[1, 2]
* m[1..3]
* m[-3, 2]
*/
static VALUE
match_aref(argc, argv, match)
int argc;
VALUE *argv;
VALUE match;
{
VALUE idx, rest;
rb_scan_args(argc, argv, "11", &idx, &rest);
if (!NIL_P(rest) || !FIXNUM_P(idx) || FIX2INT(idx) < 0) {
return rb_ary_aref(argc, argv, match_to_a(match));
}
return rb_reg_nth_match(FIX2INT(idx), match);
}