mirror of
https://github.com/nim-lang/Nim.git
synced 2026-05-23 05:09:54 +00:00
Add more typical string replace
This commit is contained in:
@@ -431,3 +431,7 @@ proc replace*(str: string, pattern: Regex,
|
||||
lastIdx = bounds.b
|
||||
|
||||
result.add(str.substr(lastIdx, str.len - 1))
|
||||
|
||||
proc replace*(str: string, pattern: Regex, sub: string): string =
|
||||
return str.replace(pattern, proc (match: RegexMatch): string =
|
||||
sub % match.captures.toSeq )
|
||||
|
||||
@@ -6,3 +6,8 @@ suite "replace":
|
||||
check("".replace(re"1", proc (v: RegexMatch): string = "1") == "")
|
||||
check(" ".replace(re"", proc (v: RegexMatch): string = "1") == "1 ")
|
||||
check("".replace(re"", proc (v: RegexMatch): string = "1") == "1")
|
||||
|
||||
test "regular replace":
|
||||
check("123".replace(re"\d", "foo") == "foofoofoo")
|
||||
check("123".replace(re"(\d)", "$1$1") == "112233")
|
||||
check("123".replace(re"(\d)(\d)", "$1$2") == "123")
|
||||
|
||||
Reference in New Issue
Block a user