Products
GG网络技术分享 2025-03-18 16:15 2
I have /components/component[name=fan/10 index=55]/cpu
I want a regex
that givens me fan/10
and 55
.
I tried stuff like =(.*)\\s
, but doesn\'t work. But I\'m guessing it has to be done using capturing groups (the () ) somehow?
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议我有 / components / component [name = fan / 10 index = 55] / cpu </ code > </ p>
我想要一个给我 fan / 10 </ code>和
55 </ code>的
regex </ code>。 </ p>
我尝试了 =(。*)\\ s </ code>之类的方法,但是没有用。 但是我猜想它必须使用捕获组(())来完成吗?</ p>
</ div>
You may use
=([^\\]\\s]+)
See regex demo
Details
=
- an equals sign ([^\\]\\s]+)
- Capturing group 1: any 1 or more chars other than ]
and whitespace.package mainimport (
\\\"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])
}
}
Output:
fan/1055
You may try to use something like this:
s := \\\"/components/component[name=fan/10 index=55]/cpu\\\"re := regexp.MustCompile(`=([^\\s\\]]*)`)
matches := re.FindAllStringSubmatch(s, -1)
fmt.Println(matches)
Result will be:
[[=fan/10 fan/10] [=55 55]]
正则表达式是匹配模式,要么匹配字符,要么匹配位置。请记住这句话。
然而关于正则如何匹配字符的学习,大部分人都觉得这块比较杂乱。
毕竟元字符太多了,看起来没有系统性,不好记。本文就解决这个问题。
内容包括:
1. 两种模糊匹配
2. 字符组
3. 量词
4. 分支结构
5. 案例分析
如果正则只有精确匹配是没多大意义的,比如/hello/,也只能匹配字符串中的"hello"这个子串。
Demand feedback