Merge pull request #2357 from reactormonk/zero-length-match
re.match returns true on zero-length match #2354
This commit is contained in:
commit
d7c39dd801
1 changed files with 21 additions and 15 deletions
|
|
@ -162,19 +162,6 @@ proc matchOrFind(s: string, pattern: Regex, start, flags: cint): cint =
|
||||||
if result >= 0'i32:
|
if result >= 0'i32:
|
||||||
result = rawMatches[1] - rawMatches[0]
|
result = rawMatches[1] - rawMatches[0]
|
||||||
|
|
||||||
proc match*(s: string, pattern: Regex, matches: var openArray[string],
|
|
||||||
start = 0): bool =
|
|
||||||
## returns ``true`` if ``s[start..]`` matches the ``pattern`` and
|
|
||||||
## the captured substrings in the array ``matches``. If it does not
|
|
||||||
## match, nothing is written into ``matches`` and ``false`` is
|
|
||||||
## returned.
|
|
||||||
return matchOrFind(s, pattern, matches, start.cint,
|
|
||||||
pcre.ANCHORED) == cint(s.len - start)
|
|
||||||
|
|
||||||
proc match*(s: string, pattern: Regex, start = 0): bool =
|
|
||||||
## returns ``true`` if ``s[start..]`` matches the ``pattern``.
|
|
||||||
return matchOrFind(s, pattern, start.cint, pcre.ANCHORED) == cint(s.len-start)
|
|
||||||
|
|
||||||
proc matchLen*(s: string, pattern: Regex, matches: var openArray[string],
|
proc matchLen*(s: string, pattern: Regex, matches: var openArray[string],
|
||||||
start = 0): int =
|
start = 0): int =
|
||||||
## the same as ``match``, but it returns the length of the match,
|
## the same as ``match``, but it returns the length of the match,
|
||||||
|
|
@ -188,6 +175,18 @@ proc matchLen*(s: string, pattern: Regex, start = 0): int =
|
||||||
## of zero can happen.
|
## of zero can happen.
|
||||||
return matchOrFind(s, pattern, start.cint, pcre.ANCHORED)
|
return matchOrFind(s, pattern, start.cint, pcre.ANCHORED)
|
||||||
|
|
||||||
|
proc match*(s: string, pattern: Regex, start = 0): bool =
|
||||||
|
## returns ``true`` if ``s[start..]`` matches the ``pattern``.
|
||||||
|
result = matchLen(s, pattern, start) != -1
|
||||||
|
|
||||||
|
proc match*(s: string, pattern: Regex, matches: var openArray[string],
|
||||||
|
start = 0): bool =
|
||||||
|
## returns ``true`` if ``s[start..]`` matches the ``pattern`` and
|
||||||
|
## the captured substrings in the array ``matches``. If it does not
|
||||||
|
## match, nothing is written into ``matches`` and ``false`` is
|
||||||
|
## returned.
|
||||||
|
result = matchLen(s, pattern, matches, start) != -1
|
||||||
|
|
||||||
proc find*(s: string, pattern: Regex, matches: var openArray[string],
|
proc find*(s: string, pattern: Regex, matches: var openArray[string],
|
||||||
start = 0): int =
|
start = 0): int =
|
||||||
## returns the starting position of ``pattern`` in ``s`` and the captured
|
## returns the starting position of ``pattern`` in ``s`` and the captured
|
||||||
|
|
@ -469,13 +468,20 @@ when isMainModule:
|
||||||
assert("var1=key; var2=key2".replace(re"(\w+)=(\w+)", "$1<-$2$2") ==
|
assert("var1=key; var2=key2".replace(re"(\w+)=(\w+)", "$1<-$2$2") ==
|
||||||
"$1<-$2$2; $1<-$2$2")
|
"$1<-$2$2; $1<-$2$2")
|
||||||
|
|
||||||
|
var accum: seq[string] = @[]
|
||||||
for word in split("00232this02939is39an22example111", re"\d+"):
|
for word in split("00232this02939is39an22example111", re"\d+"):
|
||||||
writeln(stdout, word)
|
accum.add(word)
|
||||||
|
assert(accum == @["this", "is", "an", "example"])
|
||||||
|
|
||||||
for x in findAll("abcdef", re"^{.}", 3):
|
for x in findAll("abcdef", re"^{.}", 3):
|
||||||
assert x == "d"
|
assert x == "d"
|
||||||
|
accum = @[]
|
||||||
for x in findAll("abcdef", re".", 3):
|
for x in findAll("abcdef", re".", 3):
|
||||||
echo x
|
accum.add(x)
|
||||||
|
assert(accum == @["d", "e", "f"])
|
||||||
|
|
||||||
|
assert("XYZ".find(re"^\d*") == 0)
|
||||||
|
assert("XYZ".match(re"^\d*") == true)
|
||||||
|
|
||||||
block:
|
block:
|
||||||
var matches: array[0..15, string]
|
var matches: array[0..15, string]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue