英文:
Go Regex not matching
问题
我正在尝试从自定义有效载荷中提取一个JSON对象。
我的正则表达式在这里工作得很好:https://regex101.com/r/G5lBDC/1/
但是Go语言没有返回匹配结果。
re := regexp.MustCompile(`/payload:\\"(.+?)\\\\\\"\\\\/g`)
rawResponse := "version:1 response:<status:200 message:\"OK\" payload:\"{\\\"id\\\":\\\"ll2.card_4U45pAWUMCLhsu\\\",\\\"uid\\\":\\\"ll2.acc_727V8M6GjPJ9I26CqZHgxN\\\"}\\\"\\013\\022\\006ll2-cc\\032\\0019\" endorsement:<endorser:\"\\n\\007PASSKIT\\022\\224\\010>"
rawJSON := re.FindStringSubmatch(rawResponse)
Go Playground链接:https://play.golang.org/p/XRfbExNa9V
英文:
I am trying to extract a JSON object from a custom payload.
My regex is working fine here: https://regex101.com/r/G5lBDC/1/
But Go returns no matches
re := regexp.MustCompile(`/payload:\\"(.+?)\\\\\\"\\\\/g`)
rawResponse := "version:1 response:<status:200 message:\"OK\" payload:\"{\\\"id\\\":\\\"ll2.card_4U45pAWUMCLhsu\\\",\\\"uid\\\":\\\"ll2.acc_727V8M6GjPJ9I26CqZHgxN\\\"}\\\"\3\2\re := regexp.MustCompile(`/payload:\\"(.+?)\\\\\\"\\\\/g`)
rawResponse := "version:1 response:<status:200 message:\"OK\" payload:\"{\\\"id\\\":\\\"ll2.card_4U45pAWUMCLhsu\\\",\\\"uid\\\":\\\"ll2.acc_727V8M6GjPJ9I26CqZHgxN\\\"}\\\"\\013\\022\\006ll2-cc\\032\\0019\" endorsement:<endorser:\"\\n\\007PASSKIT\\022\\224\\010>"
rawJSON := re.FindStringSubmatch(rawResponse)
6ll2-cc\2\re := regexp.MustCompile(`/payload:\\"(.+?)\\\\\\"\\\\/g`)
rawResponse := "version:1 response:<status:200 message:\"OK\" payload:\"{\\\"id\\\":\\\"ll2.card_4U45pAWUMCLhsu\\\",\\\"uid\\\":\\\"ll2.acc_727V8M6GjPJ9I26CqZHgxN\\\"}\\\"\\013\\022\\006ll2-cc\\032\\0019\" endorsement:<endorser:\"\\n\\007PASSKIT\\022\\224\\010>"
rawJSON := re.FindStringSubmatch(rawResponse)
19\" endorsement:<endorser:\"\\n\re := regexp.MustCompile(`/payload:\\"(.+?)\\\\\\"\\\\/g`)
rawResponse := "version:1 response:<status:200 message:\"OK\" payload:\"{\\\"id\\\":\\\"ll2.card_4U45pAWUMCLhsu\\\",\\\"uid\\\":\\\"ll2.acc_727V8M6GjPJ9I26CqZHgxN\\\"}\\\"\\013\\022\\006ll2-cc\\032\\0019\" endorsement:<endorser:\"\\n\\007PASSKIT\\022\\224\\010>"
rawJSON := re.FindStringSubmatch(rawResponse)
7PASSKIT\2\4\0>"
rawJSON := re.FindStringSubmatch(rawResponse)
Go Playground https://play.golang.org/p/XRfbExNa9V
答案1
得分: 1
你可以尝试这样做:(在这里运行)
package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`payload:\\"(\{[^\}]+\})`)
var str = `version:1 response:<status:200 message:\"OK\" payload:\"{\\\"id\\\":\\\"ll2.card_4U45pAWUMCLhsu\\\",\\\"uid\\\":\\\"ll2.acc_727V8M6GjPJ9I26CqZHgxN\\\"}\\\"\3\2\package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`payload:\\"(\{[^\}]+\})`)
var str = `version:1 response:<status:200 message:\"OK\" payload:\"{\\\"id\\\":\\\"ll2.card_4U45pAWUMCLhsu\\\",\\\"uid\\\":\\\"ll2.acc_727V8M6GjPJ9I26CqZHgxN\\\"}\\\"\\013\\022\\006ll2-cc\\032\\0019\" endorsement:<endorser:\"\\n\\007PASSKIT\\022\\224\\010>`
rawJSON := re.FindStringSubmatch(str)
fmt.Println(rawJSON[1])
}
6ll2-cc\2\package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`payload:\\"(\{[^\}]+\})`)
var str = `version:1 response:<status:200 message:\"OK\" payload:\"{\\\"id\\\":\\\"ll2.card_4U45pAWUMCLhsu\\\",\\\"uid\\\":\\\"ll2.acc_727V8M6GjPJ9I26CqZHgxN\\\"}\\\"\\013\\022\\006ll2-cc\\032\\0019\" endorsement:<endorser:\"\\n\\007PASSKIT\\022\\224\\010>`
rawJSON := re.FindStringSubmatch(str)
fmt.Println(rawJSON[1])
}
19\" endorsement:<endorser:\"\\n\package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`payload:\\"(\{[^\}]+\})`)
var str = `version:1 response:<status:200 message:\"OK\" payload:\"{\\\"id\\\":\\\"ll2.card_4U45pAWUMCLhsu\\\",\\\"uid\\\":\\\"ll2.acc_727V8M6GjPJ9I26CqZHgxN\\\"}\\\"\\013\\022\\006ll2-cc\\032\\0019\" endorsement:<endorser:\"\\n\\007PASSKIT\\022\\224\\010>`
rawJSON := re.FindStringSubmatch(str)
fmt.Println(rawJSON[1])
}
7PASSKIT\2\4\0>`
rawJSON := re.FindStringSubmatch(str)
fmt.Println(rawJSON[1])
}
英文:
You can try this: ( Run here )
package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`payload:\\"(\{[^\}]+\})`)
var str = `version:1 response:<status:200 message:\"OK\" payload:\"{\\\"id\\\":\\\"ll2.card_4U45pAWUMCLhsu\\\",\\\"uid\\\":\\\"ll2.acc_727V8M6GjPJ9I26CqZHgxN\\\"}\\\"\3\2\package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`payload:\\"(\{[^\}]+\})`)
var str = `version:1 response:<status:200 message:\"OK\" payload:\"{\\\"id\\\":\\\"ll2.card_4U45pAWUMCLhsu\\\",\\\"uid\\\":\\\"ll2.acc_727V8M6GjPJ9I26CqZHgxN\\\"}\\\"\\013\\022\\006ll2-cc\\032\\0019\" endorsement:<endorser:\"\\n\\007PASSKIT\\022\\224\\010>`
rawJSON := re.FindStringSubmatch(str)
fmt.Println(rawJSON[1])
}
6ll2-cc\2\package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`payload:\\"(\{[^\}]+\})`)
var str = `version:1 response:<status:200 message:\"OK\" payload:\"{\\\"id\\\":\\\"ll2.card_4U45pAWUMCLhsu\\\",\\\"uid\\\":\\\"ll2.acc_727V8M6GjPJ9I26CqZHgxN\\\"}\\\"\\013\\022\\006ll2-cc\\032\\0019\" endorsement:<endorser:\"\\n\\007PASSKIT\\022\\224\\010>`
rawJSON := re.FindStringSubmatch(str)
fmt.Println(rawJSON[1])
}
19\" endorsement:<endorser:\"\\n\package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`payload:\\"(\{[^\}]+\})`)
var str = `version:1 response:<status:200 message:\"OK\" payload:\"{\\\"id\\\":\\\"ll2.card_4U45pAWUMCLhsu\\\",\\\"uid\\\":\\\"ll2.acc_727V8M6GjPJ9I26CqZHgxN\\\"}\\\"\\013\\022\\006ll2-cc\\032\\0019\" endorsement:<endorser:\"\\n\\007PASSKIT\\022\\224\\010>`
rawJSON := re.FindStringSubmatch(str)
fmt.Println(rawJSON[1])
}
7PASSKIT\2\4\0>`
rawJSON := re.FindStringSubmatch(str)
fmt.Println(rawJSON[1])
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论