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

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

regexp get matches by group

问题

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

#subject

query="tag1 tag2"

#pattern

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

#result

tag1 tag2

#code

package main

import (
  "fmt"
  "regexp"
)

var re = regexp.MustCompile(`query="([a-z ]*)"`)

func main() {
  match  := re.FindStringSubmatch(`query="tag1 tag2"`)
  result := make(map[string]string)
  for i, name := range re.SubexpNames() {
     result[name] = match[i]
  }
  fmt.Printf("by name: %v\n", result)
}

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

#update

字符串

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

匹配结果
tag1 tag2 tag3

package main

import "fmt"
import "regexp"

func main() {
	var str string = `query="tag1 tag2 tag3" foo="wee"`
	re := regexp.MustCompile(`query="(([a-z0-9]+) ?)*"`)
	match := re.FindStringSubmatch(str)
	if len(match) == 0 {
		fmt.Print("no matches")
	} else {
		result := make(map[string]string)
		for i, name := range re.SubexpNames(){
			result[name] = match[i]
		}
		fmt.Print(result)
	}
}

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

英文:

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

#subject

query="tag1 tag2"

#pattern

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

#result

tag1 tag2

#code

package main

import (
  "fmt"
  "regexp"
)

var re = regexp.MustCompile(`query="([a-z ]*)"`)

func main() {
  match  := re.FindStringSubmatch(`query="tag1 tag2"`)
  result := make(map[string]string)
  for i, name := range re.SubexpNames() {
     result[name] = match[i]
  }
  fmt.Printf("by name: %v\n", result)
}

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

#update

string

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

matches
tag1 tag2 tag3

package main

import "fmt"
import "regexp"

func main() {
	var str string = `query="tag1 tag2 tag3" foo="wee"`
	re := regexp.MustCompile(`query="(([a-z0-9]+) ?)*"`)
	match := re.FindStringSubmatch(str)
	if len(match) == 0 {
		fmt.Print("no matches")
	} else {
		result := make(map[string]string)
		for i, name := range re.SubexpNames(){
			result[name] = match[i]
		}
		fmt.Print(result)
	}
}

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

答案1

得分: 4

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

package main

import (
	"fmt"
	"regexp"
	"strings"
)

func main() {
	var str string = `query="tag1 tag2 tag3" foo="wee"`
	re := regexp.MustCompile(`query="(.+?)"`)
	match := re.FindStringSubmatch(str)

	if len(match) == 2 {
		fmt.Println(strings.Split(match[1], " "))
	}
}

输出结果:[tag1 tag2 tag3]

英文:

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

package main

import (
	"fmt"
	"regexp"
	"strings"
)

func main() {
	var str string = `query="tag1 tag2 tag3" foo="wee"`
	re := regexp.MustCompile(`query="(.+?)"`)
	match := re.FindStringSubmatch(str)

	if len(match) == 2 {
		fmt.Println(strings.Split(match[1], " "))
	}
}

Output: [tag1 tag2 tag3]

答案2

得分: 2

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

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

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

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

输出:

Found: [tag1 tag2]

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

英文:

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

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

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

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

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:

确定