正则表达式不匹配 [英] Regexp won't match

查看:129
本文介绍了正则表达式不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以下代码

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.

这篇关于正则表达式不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆