英文:
Golang: Remove all characters except | from string
问题
我需要从字符串中删除除了"|"和空格之外的所有字符。我不知道如何在Go语言中实现这个。请帮忙。
字符串可能是这样的:
|| ||| |||| || ||||| ||| || |||| hello |
我需要它返回这样的结果:
|| ||| |||| || ||||| ||| || |||| |
提前感谢!
英文:
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!
答案1
得分: 17
我会为你翻译以下内容:
「我想,如果你唯一拥有的工具是一把锤子,那么你会倾向于把一切都当作钉子来对待。」——亚伯拉罕·马斯洛,《科学心理学》,1966年。
来自其他编程语言的程序员有时会把正则表达式视为一把锤子,把所有的文本都当作钉子来处理。
在Go语言中,保持简单和高效,例如:
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)
}
输出:
|| ||| |||| || ||||| ||| || |||| hello |
|| ||| |||| || ||||| ||| || |||| |
一个简单的基准测试:
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, "")
}
}
输出:
BenchmarkMap 5000000 337 ns/op
BenchmarkRegexp 1000000 2068 ns/op
func Map(mapping func(rune) rune, s string) string
Map 函数根据映射函数修改字符串 s 的所有字符,并返回副本。如果映射函数返回负值,则将删除该字符,不进行替换。
英文:
> "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.
答案2
得分: 9
使用regex.ReplaceAllString:
> ReplaceAllStringFunc返回src的副本,其中正则表达式的所有匹配项都被应用于匹配的子字符串的repl函数的返回值替换。 repl返回的替换项直接替换,而不使用Expand。
示例:
reg := regexp.MustCompile("[^| ]+")
origStr := "|| ||| |||| || ||||| ||| || |||| hello |"
replaceStr := reg.ReplaceAllString(origStr, "")
文档:
https://golang.org/pkg/regexp/#Regexp.ReplaceAllString
GoPlay:
https://play.golang.org/p/rfZFuQMrNJ
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论