如何将 .Net 正则表达式转换为 php


How to convert a .Net regular expression to php

我有一个正则表达式,如果短语不包含在 HTML 锚标记或 IMG 标记中,则用于替换短语。对于此示例,要搜索的短语是"hello world"

.net 正则表达式是

(?<!<a [^<]+)(?<!<img [^<]+)(?<=[ ,.;!]+)hello world(?=[ ,.;&!]+)(?!!.*</a>)

例如正则表达式应该与短语中的"hello world"匹配,例如

"one two three hello world four five"

但不应该在这样的短语中匹配你好世界

"one two three <a href='index.html'> hello world </a> four five"

"one two three <img alt='hello world' '>four five"

它与我最初开发 .Net 版本时的以下问题相关联。 与字符串不匹配的正则表达式(如果是 HTML 锚标记中的文本)

有关如何将其转换为php正则表达式的任何指导将不胜感激。

注意:请勿使用正则表达式来解析标签。

对于aimg标签,您可以执行以下操作。

(?!<(?:a|img)[^>]*?>)'bhello world'b(?![^<]*?(?:</a>|>))

观看现场演示

我想对于标签中或标签之间的任何东西,你可以试试这个。

(?!<[^>]*?>)'bhello world'b(?![^<]*?(?:</[^/]*>|>))

观看现场演示