我想将一个字符串与一片模式进行比较,看看是否有任何带有点的模式排列:
函数看起来像这样:
patterns := []string{"cat", "fish", "dog"}
func HasBadPattern(word string, patterns []string) bool {
for _, p := range patterns {
myRegex, err := regexp.Compile(p)
if err != nil {
log.Println(err)
}
//Don't know how to perpare the wordToCompile ?
matched := myRegex.FindString(wordToCompile)
if len(matched) > 0 {
return true
break
}
}
return false
}
结果应该是这样的:
HasBadPattern("c.at", patterns) //true
HasBadPattern("c.a.t", patterns) //true
HasBadPattern("c.a..t", patterns) //true
HasBadPattern("fis.h", patterns) //true
HasBadPattern("catfish", patterns) //false
HasBadPattern("here is the cat.", patterns) //false
HasBadPattern("cat is on the mat.", patterns) //false
HasBadPattern("cat.fish", patterns) //false
我的问题是如何准备word为字符之间的点编译,而不会产生误报。
尚方宝剑之说
宝慕林4294392
相关分类