正则表达式不匹配

编程入门 行业动态 更新时间:2024-10-12 12:33:22
本文介绍了正则表达式不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试以下代码

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.

更多推荐

正则表达式不匹配

本文发布于:2023-10-08 01:02:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471052.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不匹配   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!