How to match a regex with backreference in Go?

前端 未结 4 1813
北恋
北恋 2020-11-29 09:27

I need to match a regex that uses backreferences (e.g. \\1) in my Go code.

That\'s not so easy because in Go, the official regexp package uses the RE2 engine, one th

4条回答
  •  旧巷少年郎
    2020-11-29 10:01

    When I had the same problem, I solved it using a two-step regular expression match. The original code is:

    if m := match(pkgname, `^(.*)\$\{DISTNAME:S(.)(\\^?)([^:]*)(\\$?)\2([^:]*)\2(g?)\}(.*)$`); m != nil {
        before, _, left, from, right, to, mod, after := m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]
        // ...
    }
    

    The code is supposed to parse a string of the form ${DISTNAME:S|from|to|g}, which itself is a little pattern language using the familiar substitution syntax S|replace|with|.

    The two-stage code looks like this:

    if m, before, sep, subst, after := match4(pkgname, `^(.*)\$\{DISTNAME:S(.)([^\\}:]+)\}(.*)$`); m {
        qsep := regexp.QuoteMeta(sep)
        if m, left, from, right, to, mod := match5(subst, `^(\^?)([^:]*)(\$?)`+qsep+`([^:]*)`+qsep+`(g?)$`); m {
            // ...
        }
    }
    

    The match, match4 and match5 are my own wrapper around the regexp package, and they cache the compiled regular expressions so that at least the compilation time is not wasted.

提交回复
热议问题