正则不严格遵循负面的lookahead?
我有一条正则是与没有引号(双或单个)的URL匹配的 and and amp; (ampersand)在url 中。
我做的正则是我做的
([^'` "\n]+)\.([^ \n]+)&([^ "`'\n]+)(?!["'])
,但这只是不拿最后一句话,而是匹配url
https://regex101.com/ r/vpmqzh/1
google.com/cool?cool1=yes&Cool2=no&Amp;Cool3=no“
URL最终不应匹配”,
但它只是不匹配'o ' 并匹配剩余的URL。
我要做的是,如果最终存在此双重报价,那么只是不匹配整个URL。
I have a regex which is matching urls which don't have quotes (double or single) at the end and have & (ampersand) in the url.
The regex i made
([^'` "\n]+)\.([^ \n]+)&([^ "`'\n]+)(?!["'])
but it's just not taking the last word and matching the url
https://regex101.com/r/vpmqZH/1
Take the example of picture above
google.com/cool?cool1=yes&cool2=no&cool3=no"
the url should not match as it have " in the end
but it's just not matching 'o'
and matching the remaining url.
All I wanted to do is if this double quote is present in the end then just don't match the whole url.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在整个部分中使lookahead活跃。然后,我们可以选择
$
行的结尾或
(?= \ s)
空间的正lookahead。参见 https://regex101.com/r/6zgpsx/1
You need to make the lookahead active on the whole part after the ampersand. We then have the option of
$
end of the lineor
(?=\s)
positive lookahead for a space.See https://regex101.com/r/6ZGpSX/1
仅对于匹配,您可以省略捕获组,并使用否定的字符类,如果要允许匹配,则应从被否定的字符类中省略
`
。说明
[^'“ \ s。]+
匹配1+以外的非whitespace chars”
'
。'
&
&
字面上匹配[^\ s“'']+
匹配1+ 1+非whitespace chars除外,“
'
(?!\ s)
断言右侧的空间边界,请参阅a REGEX DEMO 。
For a match only, you can omit the capture groups, and use a negated character class and you should omit the backtick
`
from the negated character class if you want to allow to match it.Explanation
[^'"\s.]+
Match 1+ non whitespace chars other than"
'
.
\.
Match a dot[^\s'"&]+
Match 1+ non whitespace chars other than"
'
&
&
Match literally[^\s"']+
Match 1+ non whitespace chars other than"
'
(?!\S)
Assert a whitespace boundary to the rightSee a regex demo.