我如何匹配句子中的所有单词?词:[“测试”,“测试通知”,“结果警报”,“警报测试”]句子:“报警结果测试”我期待这样的事情[o] test[x] test noti[o] result alarm[o] alarm test我试着用文字分开,var words []stringwords = append(words, "test", "test noti", "result alarm", "alarm test")sentence := "alarm result test"for i := 0; i < len(words); i++ { log.Info(strings.Split(words[i], " "))}
1 回答
茅侃侃
TA贡献1842条经验 获得超21个赞
看看go strings包。它包含实现目标所需的功能。
举个例子:
package main
import (
"fmt"
"strings"
)
const s = "alarm result test"
var words = []string{"test", "test noti", "result alarm", "alarm test"}
func main() {
for _, w := range words {
var c bool
for _, substr := range strings.Split(w, " ") {
c = strings.Contains(s, substr)
if !c {
break
}
}
fmt.Printf("%t %s \n", c, w)
}
}
https://go.dev/play/p/PhGLePCwhho
- 1 回答
- 0 关注
- 117 浏览
添加回答
举报
0/150
提交
取消