鉴于这样一个准则:
str := "hi hey hello"
regexpr := `(?i)hello|hey|hi`
fmt.Println(regexp.MustCompile(regexpr).FindStringSubmatch(str))
它给出了这样的结果:
[hi]
但是我想得到一个[hello]
作为结果。因为在我的例子中,“你好”是第一优先级,第二优先级是“嗨”,然后是“嗨”。我怎样才能做到呢
我只知道将关键字放入切片并循环的解决方案。但它不会使用单个regexp操作
可以使用单个regexp操作吗
Tags:
您应该记住,正则表达式引擎从左到右搜索匹配项。因此,“为备选方案设置优先权”意味着“让每个备选方案在当前位置右侧的任何位置匹配”
你应该使用
这里,
.*?
将匹配除换行符以外的任何0个或更多字符,尽可能少。在代码中,使用见Go playground demo:
相关问题 更多 >
编程相关推荐