From 8497ad5b4ff0bef6c7bc42e1ed5eed9e27b8ed3f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Mon, 1 Dec 2014 16:23:47 -0500 Subject: [PATCH 1/1] lib/re: intro `Match::[]` MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/standard/re.nit | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/standard/re.nit b/lib/standard/re.nit index 20ea6b8..48142fe 100644 --- a/lib/standard/re.nit +++ b/lib/standard/re.nit @@ -442,4 +442,23 @@ redef class Match # assert match.subs.first.to_s == "d eee" # ~~~ var subs = new Array[Match] is lazy + + # Get the `n`th expression in this match + # + # `n == 0` returns this match, and a greater `n` returns the corresponding + # subexpression. + # + # Require: `n >= 0 and n <= subs.length` + # + # ~~~ + # var re = "c (d e+) f".to_re + # var match = "a b c d eee f g".search(re) + # assert match[0].to_s == "c d eee f" + # assert match[1].to_s == "d eee" + # ~~~ + fun [](n: Int): Match do + if n == 0 then return self + assert n > 0 and n <= subs.length + return subs[n-1] + end end -- 1.7.9.5