使用正则表达式来查找HTML标记特定的字符串不字符串、标记、正则表达式、HTML

2023-09-04 01:52:35 作者:慢热不讨喜

我有一个特定的正则表达式我试图用一些难度。我在寻找一个字符串的每个实例(对于我而言,我会说这是 MyString中的)的文件,除非它是一个标签,如:

I'm having some difficulty with a specific Regex I'm trying to use. I'm searching for every occurrence of a string (for my purposes, I'll say it's "mystring") in a document, EXCEPT where it's in a tag, e.g.

<a href="_mystring_">

应该不匹配,但

should not match, but

<a href="someotherstring">_mystring_</a>

应该匹配,因为它不是一个标签内(里面的意思里的&lt;和>标记)。我使用.NET的正则表达式功能这个问题,以及

Should match, since it's not inside a tag (inside meaning "inside the < and > markers") I'm using .NET's regex functions for this as well.

推荐答案

这应该做到这一点:

(?<!<[^>]*)_mystring_

它使用一个负的外观背后,以检查匹配的字符串不具有&LT;之前它没有相应的>

It uses a negative look behind to check that the matched string does not have a < before it without a corresponding >