
我正在尝试找到一种使用 Go 将模式与 regexp.Regexp 进行匹配的方法。
比赛的标准如下:
- 它必须匹配
FooBar或其子字符串Foo在行的开头,或者根本不匹配。 - 如果在 #1 中匹配,则任何一个匹配后面都必须有其他字符(即
\S+)
所以,它应该匹配,例如:
- 匹配:
FooABC - 匹配:
FooBarABC - 不匹配:
FooBar(因为后面没有其他字符) - 不匹配:
ABC(因为它不是以Foo开头)
我尝试了各种表达方式,但似乎无法理解它。
我发现其他实现中存在负向先行模式,但Go似乎没有提供它。有没有其他方法可以解决这个问题?
参见(已更新):https://regex101.com/r/SWSTzv/3
我知道这显然可以在不使用 regexp 的情况下解决。然而,这个请求的目的是了解这个问题是否可以通过 Go 的 stdlib 实现来解决。
正确答案
为什么不直接反转与正则表达式 ^Foo(?:Bar)?$ 匹配的结果(好吧,不只是)?
package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
re := regexp.MustCompile(`^Foo(?:Bar)?$`)
str := `Foo
FooBar
FooA
FooB
FooBa
FooBax
FooBxr
FooBarAbc
FooBarFoo
ABC
AbcFooBar`
for _, s := range strings.Split(str, "\n") {
if strings.HasPrefix(s, "Foo") && !re.MatchString(s) {
fmt.Println(s)
}
}
}
输出:
FooA FooB FooBa FooBax FooBxr FooBarAbc FooBarFoo
在 rextester 上尝试一下。
更新
一种更基于正则表达式并使用技巧 em>.
package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
re := regexp.MustCompile(`^Foo$|^FooBar$|^(Foo.+)`)
str := `Foo
FooBar
FooA
FooB
FooBa
FooBax
FooBxr
FooBarAbc
FooBarFoo
ABC
AbcFooBar`
for _, s := range strings.Split(str, "\n") {
submatches := re.FindStringSubmatch(s)
if submatches != nil && submatches[1] != "" {
fmt.Println(submatches[1])
}
}
}
在 rextester 上尝试一下。










