Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error parsing regexp: invalid or unsupported Perl syntax: `(?!`

Tags:

regex

go

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

error parsing regexp: invalid or unsupported Perl syntax: (?!

regexp.MustCompile("^(?!On.*On\\s.+?wrote:)(On\\s(.+?)wrote:)$"),

Can someone tell me why its not working and help me to fix this issue?

Thanks

like image 321
Giri Avatar asked Aug 13 '16 14:08

Giri


1 Answers

Go regex does not support lookarounds.

As a workaround, you may use

regexp.MustCompile(`^On\s(.+?)wrote:$`)

and

regexp.MustCompile(`^On.*On\s.+?wrote:`)

and check if the first one matches the string and the second does not.

You could also add an optional capturing group (.*On)?

regexp.MustCompile(`^On(.*On)?\s.+?wrote:`)

and check if there is a match and return true if the Group 1 ends with On - if yes, return false, else true.

like image 119
Wiktor Stribiżew Avatar answered Oct 14 '22 23:10

Wiktor Stribiżew