Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 如何从regexp.ReplaceAllFunc访问捕获组?_Go - Fatal编程技术网

Go 如何从regexp.ReplaceAllFunc访问捕获组?

Go 如何从regexp.ReplaceAllFunc访问捕获组?,go,Go,如何从ReplaceAllFunc()内部访问捕获组 目标是将[PageName]替换为 这是Go教程底部“其他任务”部分下的最后一项任务。我同意在函数内部访问capture group是理想的,我认为使用regexp.ReplaceAllFunc是不可能的。 我现在想到的唯一一件事是如何使用该函数执行此操作: package main import ( "fmt" "regexp" ) func main() { body := []byte("Visit this

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

目标是将
[PageName]
替换为


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

我同意在函数内部访问capture group是理想的,我认为使用
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))
}

您必须首先调用
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
}

如果要在
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))
}
主程序包
进口(
“fmt”
“regexp”
)
func main(){
正文:=[]字节(“访问此页面:[PageName]”)
搜索:=regexp.MustCompile(“\\[([a-zA-Z]+)\\]”)
body=search.ReplaceAllFunc(body,func(s[]字节)[]字节{
//我如何访问此处的捕获组?
组:=search.ReplaceAllString(字符串,`1`)
fmt.Println(集团)
//按您的意愿处理组
新组:=“”
返回[]字节(新组)
})
格式打印项次(字符串(正文))
}

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

是的,这也是我想到的解决方法。不过,使用捕获组可能是一个更优雅的解决方案。太糟糕了,我们不能用
ReplaceAllFunc()
…我会选择这个作为新的被接受的答案,因为它更接近我想要的。
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
}
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))
}