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?

marked as duplicate by Adrian, kostix, Martin Tournoij, Jason Aller, Flimzy Sep 1 at 9:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    Isn't this the same question? – tkausl Aug 31 at 15:14
  • What exactly are you trying to do? As patterns go, .* will match your string, but I'm guessing you're looking for something more specific than that. – Adrian Aug 31 at 15:14
  • Which part should get captured exactly? – Tpx Aug 31 at 15:14
  • So I want to diffrentiate between normal strings returned by the ` GET` command for e.g. if there is a string pair stored in redis SET key value and on GET key I get value similarly I get this kind \x01\x00\x00... of a string value when I run GET on a bitmap key. So I want to differentiate between the two to know on which data structure GET was called or to which data structure the key belongs to. @Adrian – BUTTHEAD Aug 31 at 15:21
  • 2
    Cross-post of this question asked on r/golang. – kostix Aug 31 at 15:35
up vote 0 down vote accepted

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.

An introduction to Redis data types and abstractions

Bitmaps

Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type.


Regular expressions are not the best solution. Write a simple Go function to do the conversion. For example,

package main

import (
    "fmt"
    "strconv"
)

func redisBits(s string) (string, error) {
    s, err := strconv.Unquote(`"` + s + `"`)
    if err != nil {
        return "", err
    }
    return s, nil
}

func main() {
    s := "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80J\x13\x80SQ\x80L\xe0\x80@\x92\x80L?\x80H\xe0"
    fmt.Printf("%x\n", s)
    b, err := redisBits(s)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("%x\n", b)
}

Playground: https://play.golang.org/p/rbE9iG3tOTx

Output:

0100000000000000000000804a13805351804ce0804092804c3f8048e0
0100000000000000000000804a13805351804ce0804092804c3f8048e0

Not the answer you're looking for? Browse other questions tagged or ask your own question.