Add more typical string replace

This commit is contained in:
Flaviu Tamas
2015-01-17 22:45:23 -05:00
parent 4d25a89ba9
commit 447d0e0e8b
2 changed files with 9 additions and 0 deletions

View File

@@ -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 )

View File

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