我想预编译多个正则表达式并将引用存储在变量中,但我遇到了问题。
似乎当我regexp.Compile()第二次调用该方法时,我没有得到新的参考。它指向第一个。
在下面的示例中,只有 r2 应该返回“true”。每次打电话时如何获得唯一的参考regexp.Compile()?
https://play.golang.org/p/KJJbytSZddJ
package main
import (
"fmt"
"regexp"
)
func main() {
r1, _ := regexp.Compile(`(/api)(/v\d+)/devices`)
r2, _ := regexp.Compile(`(/api)(/v\d+)/devices/\w+`)
fmt.Println(r1.MatchString("/api/v1/devices/1"))
fmt.Println(r2.MatchString("/api/v1/devices/1"))
}
动漫人物
相关分类