From: Shugo Maeda Date: 2000-09-07T23:52:07+09:00 Subject: [ruby-dev:10861] MatchingData#last_group 前田です。 昼間ぼそって話しましたが、以下のように$+に対応する番号を返すメソッ ドがMatchingDataにあると、 SYMBOL_TABLE = [] SYMBOL_TABLE[1] = :NUMBER SYMBOL_TABLE[2] = :IDENTIFIER SYMBOL_TABLE[3] = :EQUAL SYMBOL_TABLE[4] = :PLUS SYMBOL_TABLE[5] = :MINUS while line = gets while m = /(\d+)|([a-zA-Z][a-zA-Z0-9]*)|(=)|(\+)|(-)/.match(line) p SYMBOL_TABLE[m.last_group] line = $' end end みたいなのが、さくっと書けてうれしいのですがいかがでしょうか。 とりあえず、パッチです。 --- re.c 2000/09/01 09:18:11 1.32 +++ re.c 2000/09/07 14:49:10 @@ -799,6 +799,20 @@ return RMATCH(match)->str; /* str is frozen */ } +static VALUE +match_last_group(match) + VALUE match; +{ + int i; + + if (RMATCH(match)->BEG(0) == -1) return Qnil; + + for (i=RMATCH(match)->regs->num_regs-1; RMATCH(match)->BEG(i) == -1 && i > 0; i--) + ; + if (i == 0) return Qnil; + return INT2FIX(i); +} + VALUE rb_cRegexp; static void @@ -1382,5 +1396,6 @@ rb_define_method(rb_cMatch, "post_match", rb_reg_match_post, 0); rb_define_method(rb_cMatch, "to_s", match_to_s, 0); rb_define_method(rb_cMatch, "string", match_string, 0); + rb_define_method(rb_cMatch, "last_group", match_last_group, 0); rb_define_method(rb_cMatch, "inspect", rb_any_to_s, 0); } -- 前田 修吾