Golang中的SplitAfter函数适用于正则表达式类型。

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

Golang SplitAfter for Regexp type

问题

在Go语言中,strings.SplitAfter方法可以将文本在特定字符之后拆分成一个切片,但我没有找到一种使用正则表达式类型来在匹配项之后拆分文本的方法。有没有办法做到这一点?

示例:

var text string = "1.2.3.4.5.6.7.8.9"

res := strings.Split(text, ".")
fmt.Println(res) // 输出 [1 2 3 4 5 6 7 8 9]

res = strings.SplitAfter(text, ".")
fmt.Println(res) // 输出 [1. 2. 3. 4. 5. 6. 7. 8. 9]

请注意,我只提供了翻译服务,不会回答关于翻译内容的问题。

英文:

In golang strings.SplitAfter method split text after an special character into an slice, but I didn't find a way for Regexp type to split text after matches. Is there a way to do that?

Example :

var text string = "1.2.3.4.5.6.7.8.9"

res := strings.Split(text, ".")
fmt.Println(res) // print [1 2 3 4 5 6 7 8 9]

res = strings.SplitAfter(text, ".")
fmt.Println(res) // print [1. 2. 3. 4. 5. 6. 7. 8. 9]

答案1

得分: 2

首先,你的正则表达式"."splitAfter函数中是错误的。你想要匹配数字后面跟着值".",所以正确的正则表达式应该是"[1-9]"

你要找的函数可能是这样的:

func splitAfter(s string, re *regexp.Regexp) (r []string) {
	re.ReplaceAllStringFunc(s, func(x string) string {
		s = strings.Replace(s,x,"::"+x,-1)
		return s
	})
	for _, x := range strings.Split(s,"::") {
		if x != "" {
			r = append(r, x)
		}
	}
	return
}

然后:

fmt.Println(splitAfter("HealthyRecordsMetric",regexp.MustCompile("[A-Z]")))
fmt.Println(splitAfter("healthyRecordsMetric",regexp.MustCompile("[A-Z]")))
fmt.Println(splitAfter("healthyrecordsMETetric",regexp.MustCompile("[A-Z]")))
fmt.Println(splitAfter("HealthyHecord Hetrics",regexp.MustCompile("[A-Z]")))
fmt.Println(splitAfter("healthy records metric",regexp.MustCompile("[A-Z]")))
fmt.Println(splitAfter("1.2.3.4.5.6.7.8.9",regexp.MustCompile("[1-9]")))

[Healthy Records Metric]
[healthy Records Metric]
[healthyrecords M E Tetric]
[Healthy Hecord  Hetrics]
[healthy records metric]
[1. 2. 3. 4. 5. 6. 7. 8. 9]

祝你好运!

英文:

first at all, your regex "." is wrong for splitAfter function. You want number followed by value "." so the regex is: "[1-9]".

The function you are looking might look like this:

func splitAfter(s string, re *regexp.Regexp) (r []string) {
	re.ReplaceAllStringFunc(s, func(x string) string {
		s = strings.Replace(s,x,"::"+x,-1)
		return s
	})
	for _, x := range strings.Split(s,"::") {
		if x != "" {
			r = append(r, x)
		}
	}
	return
}

Than:

	fmt.Println(splitAfter("healthyRecordsMetric",regexp.MustCompile("[A-Z]")))
	fmt.Println(splitAfter("healthyrecordsMETetric",regexp.MustCompile("[A-Z]")))
	fmt.Println(splitAfter("HealthyHecord Hetrics",regexp.MustCompile("[A-Z]")))
	fmt.Println(splitAfter("healthy records metric",regexp.MustCompile("[A-Z]")))
	fmt.Println(splitAfter("1.2.3.4.5.6.7.8.9",regexp.MustCompile("[1-9]")))

[Healthy Records Metric]
[healthy Records Metric]
[healthyrecords M E Tetric]
[Healthy Hecord  Hetrics]
[healthy records metric]
[1. 2. 3. 4. 5. 6. 7. 8. 9]


Good luck!

答案2

得分: 1

Regexp类型本身没有一个确切的方法来实现这个功能,但是可以很简单地编写一个函数来实现你所要求的功能,基于Regexp的功能:

func SplitAfter(s string, re *regexp.Regexp) []string {
    var (
        r []string
        p int
    )
    is := re.FindAllStringIndex(s, -1)
    if is == nil {
        return append(r, s)
    }
    for _, i := range is {
        r = append(r, s[p:i[1]])
        p = i[1]
    }
    return append(r, s[p:])
}

这里我留下了一个可以使用的程序。

英文:

Regexp type itself does not have a method to do that exactly that but it's quite simple to write a function that implements what your asking based on Regexp functionality:

func SplitAfter(s string, re *regexp.Regexp) []string {
	var (
		r []string
		p int
	)
	is := re.FindAllStringIndex(s, -1)
    if is == nil {
	    return append(r, s)
    }
	for _, i := range is {
		r = append(r, s[p:i[1]])
		p = i[1]
	}
	return append(r, s[p:])
}

Here I left a program to play with it.

huangapple
  • 本文由 发表于 2016年12月23日 20:59:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/41301974.html
匿名

发表评论

匿名网友

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

确定