Regex 什么是正则表达式来匹配=,空格分隔后的所有事件?
我有Regex 什么是正则表达式来匹配=,空格分隔后的所有事件?,regex,go,Regex,Go,我有/components/component[name=fan/10 index=55]/cpu 我想要一个regex,它能给我fan/10和55 我尝试了类似于=(.*)s的东西,但不起作用。但我猜这必须通过使用捕获组(the())来完成吗?您可以使用 =([^\]\s]+) 看 详细信息 =-等号 ([^\]\s]+)-捕获组1:除]和空格以外的任何1个或多个字符 : package main import ( "fmt" "regexp" ) func mai
/components/component[name=fan/10 index=55]/cpu
我想要一个regex
,它能给我fan/10
和55
我尝试了类似于=(.*)s
的东西,但不起作用。但我猜这必须通过使用捕获组(the())来完成吗?您可以使用
=([^\]\s]+)
看
详细信息
-等号=
-捕获组1:除([^\]\s]+)
和空格以外的任何1个或多个字符]
package main
import (
"fmt"
"regexp"
)
func main() {
s := "/components/component[name=fan/10 index=55]/cpu"
rx := regexp.MustCompile(`=([^\]\s]+)`)
matches := rx.FindAllStringSubmatch(s, -1)
for _, v := range matches {
fmt.Println(v[1])
}
}
输出:
fan/10
55
您可以尝试使用以下内容:
s := "/components/component[name=fan/10 index=55]/cpu"
re := regexp.MustCompile(`=([^\s\]]*)`)
matches := re.FindAllStringSubmatch(s, -1)
fmt.Println(matches)
结果将是:
[[=fan/10 fan/10] [=55 55]]