Golang:从字符串中删除除了 | 之外的所有字符。

huangapple go评论94阅读模式
英文:

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

字符串包

函数 Map

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 (
	&quot;fmt&quot;
	&quot;strings&quot;
	&quot;unicode&quot;
)

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

func main() {
	s := &quot;|| ||| |||| || ||||| ||| || |||| hello |&quot;
	fmt.Println(s)
	s = remove(s)
	fmt.Println(s)
}

Output:

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

A simple benchmark:

package main

import (
	&quot;regexp&quot;
	&quot;testing&quot;
)

var (
	s = &quot;|| ||| |||| || ||||| ||| || |||| hello |&quot;
	t string
)

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

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

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(&quot;[^| ]+&quot;)
origStr := &quot;|| ||| |||| || ||||| ||| || |||| hello |&quot;
replaceStr := reg.ReplaceAllString(origStr, &quot;&quot;)

文档:
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(&quot;[^| ]+&quot;)
origStr := &quot;|| ||| |||| || ||||| ||| || |||| hello |&quot;
replaceStr := reg.ReplaceAllString(origStr, &quot;&quot;)

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

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

huangapple
  • 本文由 发表于 2017年5月17日 02:00:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/44008517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定