Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 什么是正则表达式来匹配=,空格分隔后的所有事件?_Regex_Go - Fatal编程技术网

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]+)

详细信息

  • =
    -等号
  • ([^\]\s]+)
    -捕获组1:除
    ]
    和空格以外的任何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]]