-3

Go 内置正则表达式 pkg 的文档在这里:https : //golang.org/pkg/regexp/ Go 中的正则表达式测试器在这里:https ://regoio.herokuapp.com

我有一个预定义单词的列表:

christmas, santa, tree  ( -> the order here is important. Check for words from left to right)

我正在尝试在不同的 url 字符串中检查上述单词之一:

/api/container/:containerID/santa           ( -> I want back santa)
/api/tree/:containerID/                     ( -> I want back tree)
/api/tree/:containerID/christmas            ( -> I want back christmas, not tree)

我尝试过的正则表达式是:

re := regexp.MustCompile(`^(christmas)|(santa)|(tree)$`)
      fmt.Println("santa? ", string(re.Find([]byte(`/api/container/:containerID/santa`))))
      // output OK: santa? santa
      fmt.Println("tree? ", string(re.Find([]byte(`/api/tree/:containerID/`))))  
      // output FAIL/EMPTY: tree? 
      fmt.Println("christmas? ", string(re.Find([]byte(`/api/tree/:containerID/christmas`))))  
      // output FAIL/EMPTY: christmas? 

还尝试了以下方法,但这会返回孔字符串,而不是我正在寻找的单词:

re := regexp.MustCompile(`^.*(christmas).*|.*(santa).*|.*(tree).*$`
      fmt.Println("santa? ", string(re.Find([]byte(`/api/container/:containerID/santa`))))
      // output FAIL/HOLE URL BACK: santa? /api/container/:containerID/santa
      fmt.Println("tree? ", string(re.Find([]byte(`/api/tree/:containerID/`))))  
      // output FAIL/FAIL/HOLE URL BACK: tree? /api/tree/:containerID/ 
      string(re.Find([]byte(`/api/tree/:containerID/christmas`))))  
      // output FAIL/FAIL/HOLE URL BACK: christmas? /api/tree/:containerID/christmas

我不知道正则表达式“引擎”的最后一个表达式有什么问题,应该只记住括号内的内容。

4

1 回答 1

1

不要为此任务使用正则表达式。它过于复杂,难以推理(正如您现在所知道的第一手资料),而且速度很慢。一种更简单的方法是简单地遍历每个路径段并寻找匹配项:

needles := []string{"christmas", "santa", "tree"}
sampleURL := `/api/container/:containerID/santa`
for _, part := range strings.Split(sampleURL, "/") {
    for _, needle := range needles {
        if part == needle {
            fmt.Printf("found %s\n", needle)
        }
    }
}

如果您要搜索的单词很多,则使用地图可能会提高效率:

needles := []string{"christmas", "santa", "tree", "reindeer", "bells", "chior", /* and possibly hundreds more */ }
needleMap := make(map[string]struct{}, len(needles))
for _, needle := range needles {
    needleMap[needle] = struct{}{}
}

sampleURL := `/api/container/:containerID/santa`

for _, part := range strings.Split(sampleURL, "/") {
    if _, ok := needleMap[part]; ok {
        fmt.Printf("found %s\n", needle)
    }
}
于 2020-12-11T11:09:26.997 回答