douchuituo3032 2018-08-31 15:10
浏览 13
已采纳

匹配序列化的字符串[重复]

This question already has an answer here:

I am receiving this string "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80J\x13\x80SQ\x80L\xe0\x80@\x92\x80L?\x80H\xe0" from a function (which runs a GET command on a redis bitmap and gives me the serialized string)

But due to escape sequences I am having trouble matching this kind of pattern. Can some please tell me the regex sequence that will match this kind of string?

</div>
  • 写回答

2条回答 默认 最新

  • dpzzkfb1244 2018-08-31 15:52
    关注

    First, I'd try to find out how to get that same data from Redis in its direct, binary, form (as []byte); the rest would be way more simple then.

    But to deal with this stuff in its present form, I would first normalize the input string—replacing all those backslash-escaped hex-encoded characters with their equivalent bytes.

    This would allow easily searchig for the exact values of these bytes—possibly using backslash-escaped hex-encoded characters in the patterns:

    package main

    import (
        "fmt"
        "strconv"
        "strings"
    )
    
    func main() {
        s := "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80J\x13\x80SQ\x80L\xe0\x80@\x92\x80L?\x80H\xe0"
        s, err := strconv.Unquote(`"` + s + `"`)
        if err != nil {
            panic(err)
        }
        fmt.Println(strings.Index(s, "\x80SQ\x80L"))
    }
    

    Playground link.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services