我正在尝试以下代码
d := []byte("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80J\x13\x80SQ\x80L\xe0\x80@\x92\x80L?\x80H\xe0") r := regexp.MustCompile(`(\\x[0-9][0-9].*)+`) fmt.Println(r.Match(d))但是它给了我错误,尽管在在线正则表达式测试器可以正常工作并匹配整个字符串
but it gives me false, although on an online regex tester it works fine and matches the whole string
推荐答案在字符串中使用\时,它表示转义字符序列.字符串本身不显示为\x01...,而是将每个这些块都转换为单个字符.
When you use \ in a string, it signals an escape character sequence. The string itself does not appear as \x01..., but instead, each of these chunks is converted into a single character.
也就是说,您的正则表达式试图匹配未处理的字符串值,而不是匹配存储在d中的实际字符串值.
That is to say that your regex is attempting to match the unprocessed string's value and not the actual string value stored in d.
我不确定如何改善您的正则表达式,因为我不清楚您希望它匹配什么.当前,您似乎正在尝试验证字节数组是使用特定方法实例化的,而不是通过将字节设置为整数来实例化的.如果您可以更好地表达正则表达式的目的,我也许可以进一步提供帮助.
I'm not sure how to improve your regular expression since it isn't clear to me what you expect it to match. Currently, it appears that you are attempting to validate that the byte array was instantiated using a specific method instead of by setting bytes to integers. If you can better articulate the aim of your regex, I may be able to provide assistance further.
更多推荐
正则表达式不匹配
发布评论