英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论