如何在 regexp.MatchString() 中使用单词边界 (\b)
How to use word boundry (\b) with regexp.MatchString() in go
我正在使用函数 regexp.matchString() 将正则表达式模式与我的字符串匹配。我必须使用单词边界才能找到完全匹配。例如,我想匹配 "compute" 而不是 "computer"。问题是我的字符串将同时包含 "compute" 和 "computer"。所以我想使用单词边界。我尝试在几个在线 go-regex 测试器中使用 \b 并且它有效。但是, \b 似乎不适用于 regexp.matchString() 函数。有谁知道是否有 \b 的替代品?或者我怎样才能得到预期的结果?
我的代码
package main
import "fmt"
import "regexp"
func main() {
fmt.Println("Hello, playground")
brandName := "home;compute furniture;computer"
filterVal := "(?i)compute\b"
regexMatch, _ := regexp.MatchString(filterVal, brandName)
fmt.Println(regexMatch)
}
这个函数 returns 当我使用 \b 时我错了。请帮助
双引号经常吞掉 \
。始终将 raw strings 与正则表达式、SQL 等一起使用。
filterVal := `(?i)compute\b`
我正在使用函数 regexp.matchString() 将正则表达式模式与我的字符串匹配。我必须使用单词边界才能找到完全匹配。例如,我想匹配 "compute" 而不是 "computer"。问题是我的字符串将同时包含 "compute" 和 "computer"。所以我想使用单词边界。我尝试在几个在线 go-regex 测试器中使用 \b 并且它有效。但是, \b 似乎不适用于 regexp.matchString() 函数。有谁知道是否有 \b 的替代品?或者我怎样才能得到预期的结果? 我的代码
package main
import "fmt"
import "regexp"
func main() {
fmt.Println("Hello, playground")
brandName := "home;compute furniture;computer"
filterVal := "(?i)compute\b"
regexMatch, _ := regexp.MatchString(filterVal, brandName)
fmt.Println(regexMatch)
}
这个函数 returns 当我使用 \b 时我错了。请帮助
双引号经常吞掉 \
。始终将 raw strings 与正则表达式、SQL 等一起使用。
filterVal := `(?i)compute\b`