英文:
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)
}
}
答案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 ]/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论