douyeyan0650 2018-08-31 13:36
浏览 437

正则表达式不匹配

I am trying the following code

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

  • 写回答

2条回答 默认 最新

  • douxiong1994 2018-08-31 13:40
    关注

    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.

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看