Regex replace #6006
Readf0x
started this conversation in
Ideas/Requests
Regex replace
#6006
Replies: 1 comment
-
|
Obviously lacks a bunch of features but this may get you started: package main
import "core:strings"
import "core:fmt"
import "core:text/regex"
main :: proc() {
rgx := "hellope"
replacement := "redacted"
content := "hehellope!hellope!llopewow"
iterator, err := regex.create_iterator(content, rgx)
assert(err == nil)
b: strings.Builder
off: int
for match in regex.match_iterator(&iterator) {
fmt.println(match)
assert(len(match.pos) == 1, "TODO")
pos := match.pos[0]
str := match.groups[0]
strings.write_string(&b, content[off:pos[0]])
off = pos[1]
strings.write_string(&b, replacement)
}
strings.write_string(&b, content[off:])
fmt.println(strings.to_string(b))
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've noticed that the regex package in the standard library lacks any replace procedures. This is a pretty common feature in most regex implementations that I think would greatly benefit users.
Beta Was this translation helpful? Give feedback.
All reactions