在Go中使用正则表达式拆分字符串

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

Split string using regular expression in Go

问题

我正在尝试使用正则表达式而不是字符串来拆分一个字符串,寻找一个好的方法。谢谢

http://nsf.github.io/go/strings.html?f:Split!

英文:

I'm trying to find a good way to split a string using a regular expression instead of a string. Thanks

http://nsf.github.io/go/strings.html?f:Split!

答案1

得分: 42

你可以使用regexp.Split将一个字符串按照正则表达式模式作为分隔符拆分成一个字符串切片。

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("[0-9]+")
    txt := "Have9834a908123great10891819081day!"

    split := re.Split(txt, -1)
    set := []string{}

    for i := range split {
        set = append(set, split[i])
    }

    fmt.Println(set) // ["Have", "a", "great", "day!"]
}
英文:

You can use regexp.Split to split a string into a slice of strings with the regex pattern as the delimiter.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("[0-9]+")
    txt := "Have9834a908123great10891819081day!"

    split := re.Split(txt, -1)
    set := []string{}

    for i := range split {
        set = append(set, split[i])
    }

    fmt.Println(set) // ["Have", "a", "great", "day!"]
}

答案2

得分: 18

我基于Java、C#、PHP等语言中的正则表达式分割函数的行为编写了一个正则表达式分割函数。它只返回一个字符串数组,不包含索引信息。

func RegSplit(text string, delimeter string) []string {
    reg := regexp.MustCompile(delimeter)
    indexes := reg.FindAllStringIndex(text, -1)
    laststart := 0
    result := make([]string, len(indexes) + 1)
    for i, element := range indexes {
            result[i] = text[laststart:element[0]]
            laststart = element[1]
    }
    result[len(indexes)] = text[laststart:len(text)]
    return result
}

示例:

fmt.Println(RegSplit("a1b22c333d", "[0-9]+"))

结果:

[a b c d]
英文:

I made a regex-split function based on the behavior of regex split function in java, c#, php.... It returns only an array of strings, without the index information.

func RegSplit(text string, delimeter string) []string {
    reg := regexp.MustCompile(delimeter)
    indexes := reg.FindAllStringIndex(text, -1)
    laststart := 0
    result := make([]string, len(indexes) + 1)
    for i, element := range indexes {
            result[i] = text[laststart:element[0]]
            laststart = element[1]
    }
    result[len(indexes)] = text[laststart:len(text)]
    return result
}

example:

fmt.Println(RegSplit("a1b22c333d", "[0-9]+"))

result:

[a b c d]

答案3

得分: 14

如果你只想按照特定字符进行分割,你可以使用strings.FieldsFunc,否则我会选择regexp.FindAllString

英文:

If you just want to split on certain characters, you can use strings.FieldsFunc, otherwise I'd go with regexp.FindAllString.

答案4

得分: 14

regexp.Split() 函数是实现这个功能的最佳方式。

英文:

The regexp.Split() function would be the best way to do this.

答案5

得分: 2

你应该能够创建自己的split函数,循环遍历RegExp.FindAllString的结果,并将中间的子字符串放入一个新的数组中。

http://nsf.github.com/go/regexp.html?m:Regexp.FindAllString!

英文:

You should be able to create your own split function that loops over the results of RegExp.FindAllString, placing the intervening substrings into a new array.

http://nsf.github.com/go/regexp.html?m:Regexp.FindAllString!

答案6

得分: 2

我在寻找答案时找到了这篇旧帖子。我对Go语言还不熟悉,但是这些答案对于当前版本的Go来说似乎过于复杂了。下面这个简单的函数返回与上面那些函数相同的结果。

package main

import (
	"fmt"
	"regexp"
)

func goReSplit(text string, pattern string) []string {
	regex := regexp.MustCompile(pattern)
	result := regex.Split(text, -1)
	return result
}

func main() {
	fmt.Printf("%#v\n", goReSplit("Have9834a908123great10891819081day!", "[0-9]+"))
}
英文:

I found this old post while looking for an answer. I'm new to Go but these answers seem overly complex for the current version of Go. The simple function below returns the same result as those above.

package main

import (
	"fmt"
	"regexp"
)

func goReSplit(text string, pattern string) []string {
	regex := regexp.MustCompile(pattern)
	result := regex.Split(text, -1)
	return result
}

func main() {
	fmt.Printf("%#v\n", goReSplit("Have9834a908123great10891819081day!", "[0-9]+"))
}

huangapple
  • 本文由 发表于 2010年12月17日 06:41:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/4466091.html
匿名

发表评论

匿名网友

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

确定