【发布时间】:2013-02-14 21:42:34
【问题描述】:
好的,我非常了解为什么这个正则表达式有效。我正在使用的文本是这样的:
<html>
<body>
hello
<img src="withalt" alt="hi"/>asdf
<img src="noalt" />fdsa<a href="asdf">asdf</a>
<img src="withalt2" alt="blah" />
</body>
</html>
使用以下正则表达式(在 php 中测试,但我假设它适用于所有 perl 正则表达式),它将返回所有不包含 alt 标签的 img 标签:
/<img(?:(?!alt=).)*?>/
Returns:
<img src="noalt" />
因此,基于此,我认为简单地删除无反向引用会返回相同的结果:
/<img(?!alt=).*?>/
Returns:
<img src="withalt" alt="hi"/>
<img src="noalt" />
<img src="withalt2" alt="blah" />
如您所见,它只返回所有图像标签。然后让事情变得更加混乱,删除 ? (据我所知,只是一个通配符)* 返回到最后一个>
/<img(?!alt=).*>/
Returns:
<img src="withalt" alt="hi"/>
<img src="noalt" />fdsa<a href="asdf">asdf</a>
<img src="withalt2" alt="blah" />
所以有人愿意通知我,或者至少为我指出这里发生的事情的正确方向吗?
【问题讨论】:
-
现在是 2013 年。使用 XML 解析器。
-
哈,那个 html 只是我写的很快,用来测试别人的正则表达式。
标签: regex