正则不严格遵循负面的lookahead?

发布于 2025-01-27 17:25:51 字数 712 浏览 1 评论 0原文

我有一条正则是与没有引号(双或单个)的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

enter image description here
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄离 2025-02-03 17:25:51

您需要在整个部分中使lookahead活跃。然后,我们可以选择

  • $行的结尾
  • (?= \ s)空间的正lookahead。
([^' "\n]+)\.([^ \n]+)&((?!["'])[^ "'\n])+($|(?=\s)

参见 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 line
    or
  • (?=\s) positive lookahead for a space.
([^' "\n]+)\.([^ \n]+)&((?!["'])[^ "'\n])+($|(?=\s)

See https://regex101.com/r/6ZGpSX/1

明明#如月 2025-02-03 17:25:51

仅对于匹配,您可以省略捕获组,并使用否定的字符类,如果要允许匹配,则应从被否定的字符类中省略`

[^'"\s.]+\.[^\s'"&]+&[^\s"']+(?!\S)

说明

  • [^'“ \ 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.

[^'"\s.]+\.[^\s'"&]+&[^\s"']+(?!\S)

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 right

See a regex demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文