Invalid Regex Parsing Error: Understanding "error parsing regexp: invalid or unsupported Perl syntax: (?!""
When encountering the error "error parsing regexp: invalid or unsupported Perl syntax: (?!", it signifies an issue in the regular expression syntax used in Go. This specific error occurs when using Perl's negative lookahead syntax, which Go's regexp package does not support.
In your case, the regular expression:
regexp.MustCompile("^(?!On.*On\s.+?wrote:)(On\s(.+?)wrote:)$")
attempts to match a string that does not begin with the sequence "On. On" followed by a message. However, Go does not recognize the negative lookahead syntax (?!, which is a feature specific to Perl.
Solution:
To resolve this issue, you need to find an alternative way to express the matching condition without relying on negative lookaheads. One option is to utilize two separate regular expressions:
first := regexp.MustCompile(`^On\s(.+?)wrote:$`) second := regexp.MustCompile(`^On.*On\s.+?wrote:`)
You can then use these regular expressions as follows:
if first.MatchString(str) && !second.MatchString(str) { // The string matches the desired condition }
This approach allows you to determine if the string matches the first regex (meaning it does not contain "On. On") and does not match the second regex (meaning it does not contain "On" twice).
Additional Option:
Alternatively, you can modify the original regular expression to include an optional capturing group:
regexp.MustCompile(`^On(.*On)?\s.+?wrote:`)
You can then check if there is a match and return true if the Group 1 (captured substring) ends with "On"; if yes, return false, else true.
The above is the detailed content of Why does my Go regex code throw an 'invalid or unsupported Perl syntax: (?!'' error?. For more information, please follow other related articles on the PHP Chinese website!