正则表达式捕获组在Golang中与正则表达式构建器网站的预期不同。

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

Regex Capture group does not operate as expected from regex builder website in golang

问题

基本上,我正在尝试在Golang中构建捕获组。我正在使用以下网页,该网页似乎表明我按照我编写的方式应该正常工作。

由于某些原因,这是时间敏感的,我相信你可以理解

package main
import (
    "fmt"
    "regexp"
)

func main() {
    var r = regexp.MustCompile(`/number(?P<value>.*?)into|field(?P<field>.*?)of|type(?P<type>.*?)$/g`)
    fmt.Printf("%#v\n", r.FindStringSubmatch(`cannot unmarshal number 400.50 into Go struct field MyStruct.Numbers of type int64`))
    fmt.Printf("%#v\n", r.SubexpNames())
}

当然,这产生了一个我不希望的结果,与正则表达式构建器网站上的结果不一致。这可能是因为它是为另一种语言构建的,但我对于适用于Golang并支持构建捕获组的其他网站一无所知,所以我需要帮助,因为这超出了我通常的领域。

使用我提供的正则表达式格式,上述代码的输出是:

[]string{"field", "", "", ""}
[]string{"", "value", "field", "type"}

我希望它尽可能接近以下结果:

[]string{"field", "cannot unmarshal number (number)", "into go struct (Mystruct.Numbers)", "of type (int64)"}
[]string{"", "value", "field", "type"}

就像上面的正则表达式草稿本上显示的那样。

另外,只匹配第一个匹配项会更方便。

英文:

essentially, I'm trying to build capture groups in golang. I'm utilizing the following web page, which seems to indicate that this should work properly as I've written it

For random reasons this is time sensitive, I'm sure you can sympathize

    package main
import (
    &quot;fmt&quot;
    &quot;regexp&quot;
)

func main() {
    var r = regexp.MustCompile(`/number(?P&lt;value&gt;.*?)into|field(?P&lt;field&gt;.*?)of|type(?P&lt;type&gt;.*?)$/g`)
    fmt.Printf(&quot;%#v\n&quot;, r.FindStringSubmatch(`cannot unmarshal number 400.50 into Go struct field MyStruct.Numbers of type int64`))
    fmt.Printf(&quot;%#v\n&quot;, r.SubexpNames())
}

This of course produces a result that I don't expect, which is inconsistent with the results on the regex builder website. This is probably because it was built for use with a different language, but I'm ignorant of another website that is more suited for golang that also supports building capture groups, and could use an assist on this one, as it's out of my usual wheelhouse.

the output of the above code using the regex format I have provided is

[]string{&quot;field&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;}
[]string{&quot;&quot;, &quot;value&quot;, &quot;field&quot;, &quot;type&quot;}

I'd love for it to be as close as possible to

[]string{&quot;field&quot;, &quot;cannot unmarshal number (number)&quot;, &quot;into go struct (Mystruct.Numbers)&quot;, &quot;of type (int64)&quot;}
[]string{&quot;&quot;, &quot;value&quot;, &quot;field&quot;, &quot;type&quot;}

just as it shows on the regex scratchpad above.

It would also be convenient to only match the first instance that matches.

答案1

得分: 1

这看起来像是一个XY问题

直接从json.UnmarshalTypeError中提取数据,而不是解析错误的字符串表示。

这个程序:

var v MyStruct
err := json.Unmarshal([]byte(`{"numbers": 400.50}`), &v)
if e, ok := err.(*json.UnmarshalTypeError); ok {
    fmt.Printf("Value: %s\nStruct.Field: %s\nType: %s\n",
        e.Value, e.Struct+"."+e.Field, e.Type)
}

输出结果为:

Value: number 400.50
Struct.Field: MyStruct.Numbers
Type: int64

在Go playground上运行

英文:

This looks like an XY Problem.

Extract the data directly from the json.UnmarshalTypeError instead of parsing the string representation of the error.

This program:

var v MyStruct
err := json.Unmarshal([]byte(`{&quot;numbers&quot;: 400.50}`), &amp;v)
if e, ok := err.(*json.UnmarshalTypeError); ok {
	fmt.Printf(&quot;Value: %s\nStruct.Field: %s\nType: %s\n&quot;,
		e.Value, e.Struct+&quot;.&quot;+e.Field, e.Type)
}

prints the output:

Value: number 400.50
Struct.Field: MyStruct.Numbers
Type: int64

Run it on the Go playground.

huangapple
  • 本文由 发表于 2021年6月22日 22:50:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/68085817.html
匿名

发表评论

匿名网友

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

确定