如何从regex.Replace Func访问捕获组?

时间:2015-01-17 15:12:10

标签: go

如何从ReplaceAllFunc()内部访问捕获组?

package main

import (
    "fmt"
    "regexp"
)

func main() {
    body := []byte("Visit this page: [PageName]")
    search := regexp.MustCompile("\\[([a-zA-Z]+)\\]")

    body = search.ReplaceAllFunc(body, func(s []byte) []byte {
        // How can I access the capture group here?
    })

    fmt.Println(string(body))
}

目标是将[PageName]替换为<a href="/view/PageName">PageName</a>

这是Writing Web Applications Go教程底部“其他任务”部分下的最后一项任务。

3 个答案:

答案 0 :(得分:5)

我同意在你的函数内部访问捕获组是理想的,我不认为regexp.ReplaceAllFunc是可能的。 我现在唯一想到的就是如何用这个功能做到这一点:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    body := []byte("Visit this page: [PageName] [OtherPageName]")
    search := regexp.MustCompile("\\[[a-zA-Z]+\\]")
    body = search.ReplaceAllFunc(body, func(s []byte) []byte {
        m := string(s[1 : len(s)-1])
        return []byte("<a href=\"/view/" + m + "\">" + m + "</a>")
    })
    fmt.Println(string(body))
}

修改

还有另外一种我知道如何做你想做的事。您需要知道的第一件事是您可以使用(?:re)语法指定非捕获组,其中re是您的正则表达式。这不是必需的,但会减少不感兴趣的比赛次数。

接下来要知道的是regexp.FindAllSubmatcheIndex。它将返回切片切片,其中每个内部切片表示给定匹配正则表达式的所有子匹配的范围。

有了这两件事,你可以构建一些通用的解决方案:

package main

import (
    "fmt"
    "regexp"
)

func ReplaceAllSubmatchFunc(re *regexp.Regexp, b []byte, f func(s []byte) []byte) []byte {
    idxs := re.FindAllSubmatchIndex(b, -1)
    if len(idxs) == 0 {
        return b
    }
    l := len(idxs)
    ret := append([]byte{}, b[:idxs[0][0]]...)
    for i, pair := range idxs {
        // replace internal submatch with result of user supplied function
        ret = append(ret, f(b[pair[2]:pair[3]])...)
        if i+1 < l {
            ret = append(ret, b[pair[1]:idxs[i+1][0]]...)
        }
    }
    ret = append(ret, b[idxs[len(idxs)-1][1]:]...)
    return ret
}

func main() {
    body := []byte("Visit this page: [PageName] [OtherPageName][XYZ]     [XY]")
    search := regexp.MustCompile("(?:\\[)([a-zA-Z]+)(?:\\])")

    body = ReplaceAllSubmatchFunc(search, body, func(s []byte) []byte {
        m := string(s)
        return []byte("<a href=\"/view/" + m + "\">" + m + "</a>")
    })

    fmt.Println(string(body))
}

答案 1 :(得分:2)

如果要在ReplaceAllFunc中获得分组,可以使用ReplaceAllString来获得子组。

package main

import (
    "fmt"
    "regexp"
)

func main() {
    body := []byte("Visit this page: [PageName]")
    search := regexp.MustCompile("\\[([a-zA-Z]+)\\]")

    body = search.ReplaceAllFunc(body, func(s []byte) []byte {
        // How can I access the capture group here?
        group := search.ReplaceAllString(string(s), `$1`)

        fmt.Println(group)

        // handle group as you wish
        newGroup := "<a href='/view/" + group + "'>" + group + "</a>"
        return []byte(newGroup)
    })

    fmt.Println(string(body))
}

当有很多组时,您可以通过这种方式获得每个组,然后处理每个组并返回所需的值。

答案 2 :(得分:0)

您必须先在同一个正则表达式的函数调用ReplaceAllFunc内调用FindStringSubmatch。像:

func (p parser) substituteEnvVars(data []byte) ([]byte, error) {
    var err error
    substituted := p.envVarPattern.ReplaceAllFunc(data, func(matched []byte) []byte {
        varName := p.envVarPattern.FindStringSubmatch(string(matched))[1]
        value := os.Getenv(varName)
        if len(value) == 0 {
            log.Printf("Fatal error substituting environment variable %s\n", varName)
        }

        return []byte(value)
    });
    return substituted, err
}