Add examples

This commit is contained in:
Flaviu Tamas 2015-01-18 12:04:09 -05:00
commit 79b43b19a8
2 changed files with 22 additions and 9 deletions

View file

@ -83,18 +83,24 @@ fields are as follows:
`pattern: Regex` :: the pattern that is being matched `pattern: Regex` :: the pattern that is being matched
`str: string` :: the string that was matched against `str: string` :: the string that was matched against
`captures[int|string]: string` :: the string value of whatever was captured `captures[]: string` :: the string value of whatever was captured
at that id. If the value is invalid, then behavior is undefined. If the id at that id. If the value is invalid, then behavior is undefined. If the id is
is `-1`, then the whole match is returned. If the given capture was not `-1`, then the whole match is returned. If the given capture was not matched,
matched, `nil` is returned. `nil` is returned.
`captureBounds[int|string]: Option[Slice[int]]` :: gets the bounds of the - `"abc".match(re"(\w)").captures[0] == "a"`
given capture according to the same rules as the above. If the capture is - `"abc".match(re"(?<letter>\w)").captures["letter"] == "a"`
not filled, then `None` is returned. The upper bound is exclusive, the lower - `"abc".match(re"(\w)\w").captures[-1] == "ab"`
bound is inclusive. `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. `match: string` :: the full text of the match.
`matchBounds: Slice[int]` :: the bounds of the match, as in `captureBounds[]` `matchBounds: Slice[int]` :: the bounds of the match, as in `captureBounds[]`
`(captureBounds|captures).asTable` :: returns a table with each named capture `(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. `(captureBounds|capture).toSeq` :: returns all the captures by their number.
=== `Pattern` === `Pattern`

View file

@ -4,3 +4,10 @@ suite "match":
test "upper bound must be exclusive": test "upper bound must be exclusive":
check("abc".match(re"abc", endpos = 0) == nil) check("abc".match(re"abc", endpos = 0) == nil)
check("abc".match(re"abc", endpos = 3) != nil) check("abc".match(re"abc", endpos = 3) != nil)
test "examples":
check("abc".match(re"(\w)").captures[0] == "a")
check("abc".match(re"(?<letter>\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)