VS 2015查找并替换此字符串,但不是该字符串

时间:2016-02-24 23:20:31

标签: c# regex visual-studio

我正在使用VS regex尝试删除字符串Windows.Forms但不删除System.Windows.Forms

我可以用它来查找所有行:

^(?!* System.Windows.Forms的)的 Windows.Forms的。的$

但我不能使用替换,因为它显然删除了整行。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

您只需将lookbehind调整为

即可
(?<!System\.)Windows\.Forms

或者使用单词边界更精确:

(?<!\bSystem\.)\bWindows\.Forms\b

请参阅regex demo

lookbehind是检查文本之前当前位置遇到某个子模式。因此,您需要匹配和使用的文字为Windows.Forms,但不应以System.开头。因此,lookbehind(负面的)应该只包含System\.(或\bSystem\.)。

请注意,必须对点进行转义以匹配文字点。