Add examples

This commit is contained in:
Flaviu Tamas
2015-01-18 12:04:09 -05:00
parent 39bc8c2bfb
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
`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"(?<letter>\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`

View File

@@ -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"(?<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)