通过分组获取正则表达式匹配项

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

regexp get matches by group

问题

我想解析一个字符串并获取两个引号之间的子字符串

#subject

  1. query="tag1 tag2"

#pattern

  1. query="([a-z ]*)"

#result

  1. tag1 tag2

#code

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. var re = regexp.MustCompile(`query="([a-z ]*)"`)
  7. func main() {
  8. match := re.FindStringSubmatch(`query="tag1 tag2"`)
  9. result := make(map[string]string)
  10. for i, name := range re.SubexpNames() {
  11. result[name] = match[i]
  12. }
  13. fmt.Printf("by name: %v\n", result)
  14. }

http://play.golang.org/p/voKpOlcc8J

#update

字符串

  1. query="tag1 tag2 tag3" foo="wee"

匹配结果
tag1 tag2 tag3

  1. package main
  2. import "fmt"
  3. import "regexp"
  4. func main() {
  5. var str string = `query="tag1 tag2 tag3" foo="wee"`
  6. re := regexp.MustCompile(`query="(([a-z0-9]+) ?)*"`)
  7. match := re.FindStringSubmatch(str)
  8. if len(match) == 0 {
  9. fmt.Print("no matches")
  10. } else {
  11. result := make(map[string]string)
  12. for i, name := range re.SubexpNames(){
  13. result[name] = match[i]
  14. }
  15. fmt.Print(result)
  16. }
  17. }

http://play.golang.org/p/8vieH4eDd1

英文:

I want to parse a string and get the substring inside two quotes

#subject

  1. query="tag1 tag2"

#pattern

  1. query="([a-z ]*)"

#result

  1. tag1 tag2

#code

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. var re = regexp.MustCompile(`query="([a-z ]*)"`)
  7. func main() {
  8. match := re.FindStringSubmatch(`query="tag1 tag2"`)
  9. result := make(map[string]string)
  10. for i, name := range re.SubexpNames() {
  11. result[name] = match[i]
  12. }
  13. fmt.Printf("by name: %v\n", result)
  14. }

http://play.golang.org/p/voKpOlcc8J

#update

string

  1. query="tag1 tag2 tag3" foo="wee"

matches
tag1 tag2 tag3

  1. package main
  2. import "fmt"
  3. import "regexp"
  4. func main() {
  5. var str string = `query="tag1 tag2 tag3" foo="wee"`
  6. re := regexp.MustCompile(`query="(([a-z0-9]+) ?)*"`)
  7. match := re.FindStringSubmatch(str)
  8. if len(match) == 0 {
  9. fmt.Print("no matches")
  10. } else {
  11. result := make(map[string]string)
  12. for i, name := range re.SubexpNames(){
  13. result[name] = match[i]
  14. }
  15. fmt.Print(result)
  16. }
  17. }

http://play.golang.org/p/8vieH4eDd1

答案1

得分: 4

你可以提取整个标签字符串,然后使用Split()函数进行分割。

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. )
  7. func main() {
  8. var str string = `query="tag1 tag2 tag3" foo="wee"`
  9. re := regexp.MustCompile(`query="(.+?)"`)
  10. match := re.FindStringSubmatch(str)
  11. if len(match) == 2 {
  12. fmt.Println(strings.Split(match[1], " "))
  13. }
  14. }

输出结果:[tag1 tag2 tag3]

英文:

You can extract the whole tag string and then Split() it.

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. )
  7. func main() {
  8. var str string = `query="tag1 tag2 tag3" foo="wee"`
  9. re := regexp.MustCompile(`query="(.+?)"`)
  10. match := re.FindStringSubmatch(str)
  11. if len(match) == 2 {
  12. fmt.Println(strings.Split(match[1], " "))
  13. }
  14. }

Output: [tag1 tag2 tag3]

答案2

得分: 2

首先,该模式不会匹配数字。你可能想将其更改为:

  1. var re = regexp.MustCompile(`query="(.*)"`)

然后,为了获取子字符串,你可以使用FindStringSubmatch

  1. match := re.FindStringSubmatch(`query="tag1 tag2"`)
  2. if len(match) == 2 {
  3. fmt.Printf("Found: [%s]\n", match[1])
  4. } else {
  5. fmt.Println("No match found", match)
  6. }

输出:

Found: [tag1 tag2]

然后,为了将字符串拆分为单独的标签,我建议使用strings.Split函数。

英文:

First off, the pattern will not match the digits. You might want to change it to:

  1. var re = regexp.MustCompile(`query="(.*)"`)

Then, in order to get the substring, you can just use FindStringSubmatch:

  1. match := re.FindStringSubmatch(`query="tag1 tag2"`)
  2. if len(match) == 2 {
  3. fmt.Printf("Found: [%s]\n", match[1])
  4. } else {
  5. fmt.Println("No match found", match)
  6. }

Output:

> Found: [tag1 tag2]

Then, in order to split the string into seperate tags, I recommend using strings.Split

答案3

得分: 0

正则表达式不接受数字,s/[a-z ]/[a-z0-9 ]/

http://play.golang.org/p/ov_zoxBenV

英文:

The regexp does not accept digits, s/[a-z ]/[a-z0-9 ]/

http://play.golang.org/p/ov_zoxBenV

huangapple
  • 本文由 发表于 2015年4月16日 16:54:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/29669835.html
匿名

发表评论

匿名网友

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

确定