package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?s)^((?!\bfruit\b).)* # nothing with "fruit" before
( # then, either:
# 1. fruit apple, followed by things that are not "fruit", followed by fruit banana, non-fruits, and then fruit cherry, or vice versa
\bfruit\b\ apple((?!\bfruit\b).)*(\bfruit\b\ banana((?!\bfruit\b).)*fruit\ cherry|fruit\ cherry((?!\bfruit\b).)*fruit\ banana)
|
# 2. Same with banana at the beginning
fruit\ banana((?!\\bfruit\b).)*(fruit\ apple((?!\bfruit\b).)*fruit\ cherry|fruit\ cherry((?!\bfruit\b).)*fruit\ apple)
|
# 3. And with cherry
fruit\ cherry((?!\bfruit\b).)*(fruit\ apple((?!\bfruit\b).)*fruit\ banana|fruit\ banana((?!\bfruit\b).)*fruit\ apple)
)
((?!\bfruit\b).)*$ # no more fruit`)
var str = `fruit apple
some other config line
############
fruit banana
fruit cherry
other config 1
other config`
for i, match := range re.FindAllString(str, -1) {
fmt.Println(match, "found at index", i)
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Golang, please visit: https://golang.org/pkg/regexp/