mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-18 23:11:36 +00:00
@@ -364,7 +364,8 @@ iterator findAll*(buf: cstring, pattern: Regex, start = 0, bufSize: int): string
|
||||
proc findAll*(s: string, pattern: Regex, start = 0): seq[string] {.inline.} =
|
||||
## returns all matching `substrings` of ``s`` that match ``pattern``.
|
||||
## If it does not match, @[] is returned.
|
||||
accumulateResult(findAll(s, pattern, start))
|
||||
result = @[]
|
||||
for x in findAll(s, pattern, start): result.add x
|
||||
|
||||
when not defined(nimhygiene):
|
||||
{.pragma: inject.}
|
||||
@@ -433,6 +434,7 @@ proc replace*(s: string, sub: Regex, by = ""): string =
|
||||
if match.first < 0: break
|
||||
add(result, substr(s, prev, match.first-1))
|
||||
add(result, by)
|
||||
if match.last + 1 == prev: break
|
||||
prev = match.last + 1
|
||||
add(result, substr(s, prev))
|
||||
|
||||
@@ -458,6 +460,7 @@ proc replacef*(s: string, sub: Regex, by: string): string =
|
||||
if match.first < 0: break
|
||||
add(result, substr(s, prev, match.first-1))
|
||||
addf(result, by, caps)
|
||||
if match.last + 1 == prev: break
|
||||
prev = match.last + 1
|
||||
add(result, substr(s, prev))
|
||||
|
||||
@@ -543,7 +546,8 @@ proc split*(s: string, sep: Regex, maxsplit = -1): seq[string] {.inline.} =
|
||||
## Splits the string ``s`` into a seq of substrings.
|
||||
##
|
||||
## The portion matched by ``sep`` is not returned.
|
||||
accumulateResult(split(s, sep))
|
||||
result = @[]
|
||||
for x in split(s, sep): result.add x
|
||||
|
||||
proc escapeRe*(s: string): string =
|
||||
## escapes ``s`` so that it is matched verbatim when used as a regular
|
||||
@@ -674,3 +678,9 @@ when isMainModule:
|
||||
accum.add($x)
|
||||
doAssert(accum == @["a","b","c"])
|
||||
|
||||
block:
|
||||
# bug #9306
|
||||
doAssert replace("bar", re"^", "foo") == "foobar"
|
||||
doAssert replace("foo", re"", "-") == "-foo"
|
||||
doAssert replace("foo", re"$", "bar") == "foobar"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user