Search code examples
regexgoregex-lookarounds

Error parsing regexp: invalid or unsupported Perl syntax: `?=`


When I try this regex in Golang I'm getting a regex parsing error.

panic: regexp: Compile(.+?(?=someText)): error parsing regexp: invalid or unsupported Perl syntax: (?=

regexp.MustCompile(`.+?(?=someText)`)

This is probably something to do with lookarounds, but I'm not sure about what the solution is as I don't have much experience with regex.


Solution

  • Judging by the first question mark, it seems like you're trying to get the substring before the first occurrence of someText. If that is in fact the case, you could use this as an alternative:

    package main
    import "strings"
    
    func main() {
       s := "March someText April someText"
       n := strings.Index(s, "someText")
       s = s[:n]
       println(s == "March ")
    }
    

    https://golang.org/pkg/strings#Index