Golang:删除除|以外的所有字符 来自字符串[关闭]

I need to Remove all characters except "|" and whitespace from a string. I don't understand how to do this in Go. Please help.

The string could look like this:

|| ||| |||| || ||||| ||| || |||| hello |

and I need it to return this:

|| ||| |||| || ||||| ||| || ||||  |

Thanks in advance!

Use regex.ReplaceAllString:

ReplaceAllStringFunc returns a copy of src in which all matches of the Regexp have been replaced by the return value of function repl applied to the matched substring. The replacement returned by repl is substituted directly, without using Expand.

Example:

reg := regexp.MustCompile("[^| ]+")
origStr := "|| ||| |||| || ||||| ||| || |||| hello |"
replaceStr := reg.ReplaceAllString(origStr, "")

Docs: https://golang.org/pkg/regexp/#Regexp.ReplaceAllString

GoPlay: https://play.golang.org/p/rfZFuQMrNJ

"I suppose it is tempting, if the only tool you have is a hammer, to treat everything as if it were a nail." Abrahanm Maslow, The Psychology of Science, 1966.

Programmers from other languages sometimes think of regular expressions as a hammer and treat all text as a nail.

In Go, keep it simple and efficient, for example,

package main

import (
    "fmt"
    "strings"
    "unicode"
)

func remove(s string) string {
    return strings.Map(
        func(r rune) rune {
            if r == '|' || unicode.IsSpace(r) {
                return r
            }
            return -1
        },
        s,
    )
}

func main() {
    s := "|| ||| |||| || ||||| ||| || |||| hello |"
    fmt.Println(s)
    s = remove(s)
    fmt.Println(s)
}

Output:

|| ||| |||| || ||||| ||| || |||| hello |
|| ||| |||| || ||||| ||| || ||||  |

A simple benchmark:

package main

import (
    "regexp"
    "testing"
)

var (
    s = "|| ||| |||| || ||||| ||| || |||| hello |"
    t string
)

func BenchmarkMap(b *testing.B) {
    for i := 0; i < b.N; i++ {
        t = remove(s)
    }
}

func BenchmarkRegexp(b *testing.B) {
    reg := regexp.MustCompile("[^| ]+")
    for i := 0; i < b.N; i++ {
        t = reg.ReplaceAllString(s, "")
    }
}

Output:

BenchmarkMap         5000000           337 ns/op
BenchmarkRegexp      1000000          2068 ns/op

Package strings

func Map

func Map(mapping func(rune) rune, s string) string

Map returns a copy of the string s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement.