Add more typical string replace
This commit is contained in:
parent
4d25a89ba9
commit
447d0e0e8b
2 changed files with 9 additions and 0 deletions
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue