错误解析regexp:无效或不支持的Perl语法:`(?!` [英] error parsing regexp: invalid or unsupported Perl syntax: `(?!`
问题描述
当我在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。+?写道:)
$如果组1以
On $ c $结尾,则检查是否存在匹配并返回 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屋!