How is unescape a shielded regular expression?

I am looking for a regexp.QuoteMeta backlink . Is there such a function?

I tried to manually cancel it with strings.Replace("\", "", -1), but this is error prone / unreliable, as it does not work in all cases (i.e. with excessive escaping or unicode).

I also tried adding some quotes and using strconv.Unquote (e.g. strconv.Unquote( " + "https:\/\/ad.doubleclick.net" +" ) ), but it is wrong,

+4
source share
1 answer

Parse the line with regexp / syntax to get a line without quotes:

func unquoteMeta(s string) (string, error) {
    r, err := syntax.Parse(s), 0)
    if err != nil {
        return "", err
    }
    if r.Op != syntax.OpLiteral {
        return "", errors.New("not a quoted meta")
    }
    return string(r.Rune), nil
}

playground example

+3
source

Source: https://habr.com/ru/post/1690551/


All Articles