When attempting to use a regular expression in Go, an error is encountered:
error parsing regexp: invalid or unsupported Perl syntax: (?!
This error occurs specifically with the following regex:
regexp.MustCompile("^(?!On.*On\s.+?wrote:)(On\s(.+?)wrote:)$")
The issue arises because Go regex doesn't support lookarounds, unlike Perl. Lookarounds are assertions that check the surrounding text without consuming it.
To work around this limitation, use a different approach:
Firstly, compile two separate regular expressions:
first := regexp.MustCompile(`^On\s(.+?)wrote:$`) second := regexp.MustCompile(`^On.*On\s.+?wrote:`)
Then, perform the following steps:
Alternatively, you can use an optional capturing group to simplify the process:
regex := regexp.MustCompile(`^On(.*On)?\s.+?wrote:`)
Check for a match and return true if:
Otherwise, return false.
The above is the detailed content of How to Work Around Lookarounds in Go Regex?. For more information, please follow other related articles on the PHP Chinese website!