【问题标题】:Is there any way of using the not (^) in regex for multiple characters?有没有办法在正则表达式中使用 not (^) 来处理多个字符?
【发布时间】:2011-07-07 00:51:20
【问题描述】:

我想制作一个匹配所有相关补丁的正则表达式模式。

我要匹配的内容:

img src="图片.png" img src="http_image.png"

我不想匹配的内容:

img src="http://example/image.png"

我尝试匹配这些模式,但它们都不起作用:

\src="[^http://]\ \src="^(http://)\ \src="[^h][^t][^t][^p][^:][^/][^/]\ \src="([^h][^t][^t][^p][^:][^/][^/])\

我把 留在了 img 标记之外,因为否则我无法将它写成代码。
src 属性将始终使用双引号 (") 而不是单引号 (') 进行格式化。
它总是包含一个“http”源,而不是“https”或其他。

【问题讨论】:

  • 您不了解^ 的工作原理。它用于否定字符类中的字符。那么[^http://] 的真正含义是“匹配任何不是htp:/ 的字符”
  • 我知道...但我只是举了一些例子来帮助人们理解我想做什么

标签: regex string regex-negation


【解决方案1】:

解决这个问题的方法是使用否定的前瞻断言:

^img src="(?!http:\/\/\S+).*"$

Rubular link

^                  : Start anchor
img src="          : Literal img src="
(?!http:\/\/\S+)   : To ensure the string following " is not a URL
.*                 : Anything else is allowed
"                  : Literal "
$                  : End anchor

【讨论】:

  • TYVM!顺便说一句,您知道如何在 stackoverflow 中添加 img src="example/image.png" (带括号)并使其显示为文本吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-28
  • 2021-03-19
  • 1970-01-01
  • 2023-03-17
  • 2019-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多