Golang按字符类型拆分字符串

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

Golang split strings by char type

问题

我想要能够将字符串分割成字符和数字分开的子字符串组:

re:=regexp.MustCompile("**MAGIC HERE**")
fmt.Println(re.FindAllString("abc123def", -1))

我想要得到

[abc 123 def]

有什么想法吗?

英文:

I want to be able to split the string into substrings where characters and numbers are separate groups:

re:=regexp.MustCompile("**MAGIC HERE**")
fmt.Println(re.FindAllString("abc123def", -1))

I want to be able to get

[abc 123 def]

Any ideas?

答案1

得分: 4

尝试使用以下模式进行拆分:

\d+|\D+

代码:

re:=regexp.MustCompile("\\d+|\\D+")
fmt.Println(re.FindAllString("abc123def", -1))

输出:

[abc 123 def]

演示在此处:

Rextester

英文:

Try splitting on this pattern:

\d+|\D+

Code:

re:=regexp.MustCompile("\\d+|\\D+")
fmt.Println(re.FindAllString("abc123def", -1))

Output:

[abc 123 def]

Demo here:

<h1>Rextester</h1>

huangapple
  • 本文由 发表于 2017年9月18日 07:59:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/46269946.html
匿名

发表评论

匿名网友

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

确定