我有这些字符串,他们可以有多种方式,如:
id=PS\\ Old\\ Gen, value=34
和id=Code\\ Cache,value=22
等。
我想正则表达式,将在=
到,
所以基本上之后提取任何东西:PS\\ Old\\ Gen
和Code\\ Cache
等。
我写了下面的正则表达式,但似乎无法在,
之前得到的最后一个字。
(([a-zA-z]+)\\{2})+
有什么想法吗?这是围棋的语言。
你可以使用这个表达式和1组捕获你的文字,
id=([^,=]*),
说明:
id=
- 匹配id=
字面上([^,=]*)
- 匹配的任何字符以外,
或=
零次或多次,并在第一分组模式捕捉,
- 匹配一个逗号样品围棋代码,
var re = regexp.MustCompile(`id=([^,=]*),`)
var str = `id=PS\\ Old\\ Gen, value=34 id=Code\\ Cache,value=22`
res := re.FindAllStringSubmatch(str, -1)
for i := range res {
fmt.Printf("Match: %s\n", res[i][1])
}
打印,
Match: PS\\ Old\\ Gen
Match: Code\\ Cache
是否像id=([^,]+),
做的伎俩?
捕获组一号将包含你的对手。看到这个动作here
那个怎么样?查阅REGEX
package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?mi)id=([^,]+)`)
var str = `id=PS\\ Old\\ Gen, value=34 and id=Code\\ Cache,value=22`
for i, match := range re.FindAllString(str, -1) {
fmt.Println(match, "found at index", i)
}
}