【发布时间】:2013-10-07 13:36:41
【问题描述】:
如果短语不包含在 HTML 锚标记或 IMG 标记中,我有一个用于替换短语的正则表达式。在本例中,要搜索的短语是“hello world”
.net 正则表达式是
(?<!<a [^<]+)(?<!<img [^<]+)(?<=[ ,.;!]+)hello world(?=[ ,.;&!]+)(?!!.*</a>)
例如正则表达式应匹配短语中的“hello world”,如
"one two three hello world four five"
但不应匹配 hello world 之类的短语
"one two three <a href='index.html'> hello world </a> four five"
或
"one two three <img alt='hello world' \>four five"
它与我最初开发 .Net 版本时的以下问题有关。 regular expression that doesn't match a string if it's the text within an html anchor tag
非常感谢任何有关如何将其转换为 php 正则表达式的指导。
【问题讨论】:
-
这个正则表达式也不应该匹配 .NET 中的任何内容。结束标记有一个斜杠 (
</a>),但您的表达式会查找反斜杠。 -
@DanielHilgarth 感谢您的更正,我已经更新了问题。
-
现在有什么问题?
-
它不是在寻找图像标签的结束“>”吗?
-
这与 .NET 与 PHP 有什么关系?您的正则表达式根本不包含会寻找类似内容的部分......