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

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

Golang SplitAfter for Regexp type

问题

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

示例:

  1. var text string = "1.2.3.4.5.6.7.8.9"
  2. res := strings.Split(text, ".")
  3. fmt.Println(res) // 输出 [1 2 3 4 5 6 7 8 9]
  4. res = strings.SplitAfter(text, ".")
  5. 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 :

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

答案1

得分: 2

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

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

  1. func splitAfter(s string, re *regexp.Regexp) (r []string) {
  2. re.ReplaceAllStringFunc(s, func(x string) string {
  3. s = strings.Replace(s,x,"::"+x,-1)
  4. return s
  5. })
  6. for _, x := range strings.Split(s,"::") {
  7. if x != "" {
  8. r = append(r, x)
  9. }
  10. }
  11. return
  12. }

然后:

  1. fmt.Println(splitAfter("HealthyRecordsMetric",regexp.MustCompile("[A-Z]")))
  2. fmt.Println(splitAfter("healthyRecordsMetric",regexp.MustCompile("[A-Z]")))
  3. fmt.Println(splitAfter("healthyrecordsMETetric",regexp.MustCompile("[A-Z]")))
  4. fmt.Println(splitAfter("HealthyHecord Hetrics",regexp.MustCompile("[A-Z]")))
  5. fmt.Println(splitAfter("healthy records metric",regexp.MustCompile("[A-Z]")))
  6. fmt.Println(splitAfter("1.2.3.4.5.6.7.8.9",regexp.MustCompile("[1-9]")))
  7. [Healthy Records Metric]
  8. [healthy Records Metric]
  9. [healthyrecords M E Tetric]
  10. [Healthy Hecord Hetrics]
  11. [healthy records metric]
  12. [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:

  1. func splitAfter(s string, re *regexp.Regexp) (r []string) {
  2. re.ReplaceAllStringFunc(s, func(x string) string {
  3. s = strings.Replace(s,x,"::"+x,-1)
  4. return s
  5. })
  6. for _, x := range strings.Split(s,"::") {
  7. if x != "" {
  8. r = append(r, x)
  9. }
  10. }
  11. return
  12. }

Than:

  1. fmt.Println(splitAfter("healthyRecordsMetric",regexp.MustCompile("[A-Z]")))
  2. fmt.Println(splitAfter("healthyrecordsMETetric",regexp.MustCompile("[A-Z]")))
  3. fmt.Println(splitAfter("HealthyHecord Hetrics",regexp.MustCompile("[A-Z]")))
  4. fmt.Println(splitAfter("healthy records metric",regexp.MustCompile("[A-Z]")))
  5. fmt.Println(splitAfter("1.2.3.4.5.6.7.8.9",regexp.MustCompile("[1-9]")))
  6. [Healthy Records Metric]
  7. [healthy Records Metric]
  8. [healthyrecords M E Tetric]
  9. [Healthy Hecord Hetrics]
  10. [healthy records metric]
  11. [1. 2. 3. 4. 5. 6. 7. 8. 9]

Good luck!

答案2

得分: 1

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

  1. func SplitAfter(s string, re *regexp.Regexp) []string {
  2. var (
  3. r []string
  4. p int
  5. )
  6. is := re.FindAllStringIndex(s, -1)
  7. if is == nil {
  8. return append(r, s)
  9. }
  10. for _, i := range is {
  11. r = append(r, s[p:i[1]])
  12. p = i[1]
  13. }
  14. return append(r, s[p:])
  15. }

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

英文:

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:

  1. func SplitAfter(s string, re *regexp.Regexp) []string {
  2. var (
  3. r []string
  4. p int
  5. )
  6. is := re.FindAllStringIndex(s, -1)
  7. if is == nil {
  8. return append(r, s)
  9. }
  10. for _, i := range is {
  11. r = append(r, s[p:i[1]])
  12. p = i[1]
  13. }
  14. return append(r, s[p:])
  15. }

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:

确定