我有/components/component[name=fan/10 index=55]/cpu我想要一個regex給我fan/10和的55。我嘗試過類似的東西=(.*)\s,但不起作用。但我猜它必須以某種方式使用捕獲組( () )來完成?
2 回答

呼喚遠方
TA貢獻1856條經驗 獲得超11個贊
您可以使用
=([^\]\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

慕俠2389804
TA貢獻1719條經驗 獲得超6個贊
您可以嘗試使用這樣的東西:
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]]
- 2 回答
- 0 關注
- 151 瀏覽
添加回答
舉報
0/150
提交
取消