diff --git a/README.asciidoc b/README.asciidoc index d16f9e0ac9..f4a87b65aa 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -83,18 +83,24 @@ fields are as follows: `pattern: Regex` :: the pattern that is being matched `str: string` :: the string that was matched against -`captures[int|string]: string` :: the string value of whatever was captured - at that id. If the value is invalid, then behavior is undefined. If the id - is `-1`, then the whole match is returned. If the given capture was not - matched, `nil` is returned. -`captureBounds[int|string]: Option[Slice[int]]` :: gets the bounds of the - given capture according to the same rules as the above. If the capture is - not filled, then `None` is returned. The upper bound is exclusive, the lower - bound is inclusive. +`captures[]: string` :: the string value of whatever was captured +at that id. If the value is invalid, then behavior is undefined. If the id is +`-1`, then the whole match is returned. If the given capture was not matched, +`nil` is returned. + - `"abc".match(re"(\w)").captures[0] == "a"` + - `"abc".match(re"(?\w)").captures["letter"] == "a"` + - `"abc".match(re"(\w)\w").captures[-1] == "ab"` +`captureBounds[]: Option[Slice[int]]` :: gets the bounds of the +given capture according to the same rules as the above. If the capture is not +filled, then `None` is returned. The upper bound is exclusive, the lower bound +is inclusive. + - `"abc".match(re"(\w)").captureBounds[0] == 0..1` + - `"abc".match(re"").captureBounds[-1] == 0..0` + - `"abc".match(re"abc").captureBounds[-1] == 0..3` `match: string` :: the full text of the match. `matchBounds: Slice[int]` :: the bounds of the match, as in `captureBounds[]` `(captureBounds|captures).asTable` :: returns a table with each named capture - as a key. +as a key. `(captureBounds|capture).toSeq` :: returns all the captures by their number. === `Pattern` diff --git a/test/match.nim b/test/match.nim index b3cf1c5e50..7fa00610ae 100644 --- a/test/match.nim +++ b/test/match.nim @@ -4,3 +4,10 @@ suite "match": test "upper bound must be exclusive": check("abc".match(re"abc", endpos = 0) == nil) check("abc".match(re"abc", endpos = 3) != nil) + test "examples": + check("abc".match(re"(\w)").captures[0] == "a") + check("abc".match(re"(?\w)").captures["letter"] == "a") + check("abc".match(re"(\w)\w").captures[-1] == "ab") + check("abc".match(re"(\w)").captureBounds[0].get == 0..1) + check("abc".match(re"").captureBounds[-1].get == 0..0) + check("abc".match(re"abc").captureBounds[-1].get == 0..3)