package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?m)\bKEYWORD1\b(?:(?<=\bKeyWord2\b.*?)|(?=.*?\bKeyWord2\b))`)
var str = `KEYWORD1 is ahead of Keyword2
KEYWORD1 is ahead of Keyword2 and another KeyWord2
KeyWord2 is behind KEYWORD1
KEYWORD1 is ahead of KEYWORD1 and behind KeyWord2
KeyWord2 is twice of KeyWord2 and in the end KEYWORD1
KEYWORD1 is
`
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/