需要变宽的负向后方替换 [英] Need variable width negative lookbehind replacement
问题描述
我在这里查看了许多问题(以及许多其他网站),并且提供了一些提示,但没有一个给出明确的答案.我知道正则表达式,但我远不是一名专家.这个特定的问题与PHP中的regex有关.
I have looked at many questions here (and many more websites) and some provided hints but none gave me a definitive answer. I know regular expressions but I am far from being a guru. This particular question deals with regex in PHP.
我需要在文本中找到没有被给定类的超链接包围的单词.例如,我可能有
I need to locate words in a text that are not surrounded by a hyperlink of a given class. For example, I might have
This <a href="blabblah" class="no_check">elephant</a> is green and this elephant is blue while this <a href="blahblah">elephant</a> is red.
我需要与第二和第三只大象比赛,而不是第一只(由测试类"no_check"标识).请注意,除了超链接中的href和class外,还有更多的属性.我想出了
I would need to match against the second and third elephants but not the first (identified by test class "no_check"). Note that there could more attributes than just href and class within hyperlinks. I came up with
((?<!<a .*class="no_check".*>)\belephant\b)
在regex测试软件中可以很好地工作,而在PHP中则不能.
which works beautifully in regex test software but not in PHP.
任何帮助将不胜感激.如果您不能提供正则表达式,但是可以找到某种PHP代码逻辑来规避对它的需要,我将不胜感激.
Any help is greatly appreciated. If you cannot provide a regular expression but can find some sort of PHP code logic that would circumvent the need for it, I would be equally grateful.
推荐答案
我最终使用了混合解决方案.事实证明,我必须为特定的关键字解析文本,并检查它们是否已成为链接的一部分,如果没有,则将其添加到超链接中.这里提供的解决方案非常有趣,但不能完全满足我的需求.
I ended up using a mixed solution. It turns out that I had to parse a text for specific keywords and check if they were already part of a link and if not add them to a hyperlink. The solutions provided here were very interesting but not exactly tailored enough for what I needed.
使用HTML解析器的想法虽然不错,但我目前正在另一个项目中使用它.因此,对艾伦·摩尔和埃里克·斯特罗姆提出的解决方案表示敬意.
The idea of using an HTML parser was a good one though and I am currently using one in another project. So hats off to both Alan Moore and Eric Strom for suggesting that solution.
这篇关于需要变宽的负向后方替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!