Go正则表达式不匹配

huangapple go评论152阅读模式
英文:

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]) }

huangapple
  • 本文由 发表于 2017年7月17日 17:17:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/45140270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定