C# 使用正则表达式,如何替换特定html标记中未包含的文本?

C# 使用正则表达式,如何替换特定html标记中未包含的文本?,c#,regex,C#,Regex,假设我有这段文字: The quick brown fox <a href="http://www.example1.com">jumps over</a> the <a href="http://www.example2.com">lazy</a> dog 敏捷的棕色狐狸和狗 除了XXXXXXXX之间的字符外,我如何将所有字符替换为X 您可以使用此模式,它将查找不在标记之间的每个字符: (?:\G|(?<=</a>))(?:

假设我有这段文字:

The quick brown fox <a href="http://www.example1.com">jumps over</a> the <a href="http://www.example2.com">lazy</a> dog
敏捷的棕色狐狸和狗 除了XXXXXXXX之间的字符外,我如何将所有字符替换为X
您可以使用此模式,它将查找不在
标记之间的每个字符:

(?:\G|(?<=</a>))(?:[^<]|<(?!a\b))

谢谢你的回答。我不知道\G为什么需要打开非捕获组?捕获组也可以工作,不是吗?@Anthony:的确,您可以对捕获组执行相同的操作,但由于您不需要捕获任何内容,非捕获组可以避免无偿使用内存。@Anthony:同样,您可以用原子组替换非捕获组
(?>…)
因为正则表达式引擎不需要记录回溯位置。
(?:\G|(?<=</a>))(?:[^<]|<(?!a\b))
(?:            # open a non capturing group
    \G         # contiguous to precedent match or the begining of the string
  |            # OR
    (?<=</a>)  # preceded by the closing "a" tags
)              # close the non capturing group
(?:            # open a non capturing group
    [^<]       # all that is not a <
  |            # OR
    <(?!a\b)   # < not followed by "a" (=not a "a" tag)
)              # close the non capturing group
(?:\G|(?<=</a>))(?:\r\n|[^<]|<(?!a\b))