错误解析regexp:无效或不支持的Perl语法:`(?!` [英] error parsing regexp: invalid or unsupported Perl syntax: `(?!`

查看:7197
本文介绍了错误解析regexp:无效或不支持的Perl语法:`(?!`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在golang中尝试这个正则表达式时,我得到了正则表达式解析错误。
$ b


错误解析正则表达式:无效或不支持的Perl语法:(?!




  regexp.MustCompile(^(?! On。* On\\s。+ ?写道:)(On \\s(。+?)写道:)$),



<有人可以告诉我为什么它不起作用,并帮助我解决这个问题吗?



谢谢

解决方案

Go正则表达式不支持lookaround。



作为一种解决方法,您可以使用

  regexp.MustCompile(^ On \\s(。+?)写道:$)

  regexp.MustCompile(^ On。* On\ \s。+?写:)

并检查第一个匹配字符串和第二个不会。



您还可以添加一个可选的捕获组(。* On)?

  regexp.MustCompile(^ On(。* On)?\\ s。+?写道:)
On true 。如果是,返回 false ,否则 true


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

解决方案

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.

这篇关于错误解析regexp:无效或不支持的Perl语法:`(?!`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆