What would be a non-regexp alternative?

97 views
Skip to first unread message

Vivi

unread,
May 3, 2020, 11:10:35 PM5/3/20
to golang-nuts
I find there're large chuck of strings would need to be scanned and remove/clean up, are there alternative way to improve beside using regexp? Replacer/NewReplacer seems to be limit on exact characters or my idea of NewReplaceGroup could be useful.

([abc .#$]+)

Tamás Gulácsi

unread,
May 4, 2020, 2:24:26 AM5/4/20
to golang-nuts
You just want to remove those characters? Use strings.Map.

Vivi

unread,
May 4, 2020, 5:26:26 AM5/4/20
to golang-nuts
I like to replace one or more characters to one character e.g. to sanitize proper slug title with a dash.

Tamás Gulácsi

unread,
May 4, 2020, 5:50:15 AM5/4/20
to golang-nuts

Vivi

unread,
May 4, 2020, 6:23:05 AM5/4/20
to golang-nuts
Interesting, I didn't know strings.Map could work this way, I have done a performance benchmark with PHP 7 Pcre2 C is 1,020 nanoseconds and strings.Map version is 2,888 nanoseconds, not bad improvement over regexp. If there is a better approach, however, Rust Re2 is just as fast as PHP Pcre2.

Tamás Gulácsi

unread,
May 4, 2020, 8:06:19 AM5/4/20
to golang-nuts
bytes.Map may be faster.
But even that is just a handful of lines (https://github.com/golang/go/blob/go1.14.2/src/bytes/bytes.go#L453)
so if you really want speed, just copy and tweak it.

I'm not saying that regexps are slow, but they're complex, and where
a simple loop with a boolean placeholder satisfies, then we don't need  that regexp.
Reply all
Reply to author
Forward
0 new messages