正则表达式查找字符串和反斜杠

I have these strings and they can come in a variety of ways such as:

id=PS\\ Old\\ Gen, value=34 and id=Code\\ Cache,value=22 etc.

I would like a regex that would extract anything after the = to the , so basically: PS\\ Old\\ Gen and Code\\ Cache etc.

I have written the following regex but can't seem to get the last word before the ,.

(([a-zA-z]+)\\{2})+

Any thoughts? This is for go language.

You can use this regex and capture your text from group1,

id=([^,=]*),

Explanation:

  • id= - Matches id= literally
  • ([^,=]*) - Matches any character except , or = zero or more times and captures in first grouping pattern
  • , - Matches a comma

Demo

Sample Go codes,

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
", res[i][1])
}

Prints,

Match: PS\\ Old\\ Gen
Match: Code\\ Cache

Does something like id=([^,]+), do the trick?

Capture group no.1 will contain your match. See this in action here

How about that? SEE 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)
    }
}