英文:
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 (
"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())
}
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{"field", "", "", ""}
[]string{"", "value", "field", "type"}
I'd love for it to be as close as possible to
[]string{"field", "cannot unmarshal number (number)", "into go struct (Mystruct.Numbers)", "of type (int64)"}
[]string{"", "value", "field", "type"}
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
英文:
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(`{"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)
}
prints the output:
Value: number 400.50
Struct.Field: MyStruct.Numbers
Type: int64
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论