Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# VS 2015查找并替换此字符串,但不替换该字符串_C#_Regex_Visual Studio - Fatal编程技术网

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

C# VS 2015查找并替换此字符串,但不替换该字符串,c#,regex,visual-studio,C#,Regex,Visual Studio,我正在使用VS regex尝试删除字符串Windows.Forms,但不是System.Windows.Forms 我可以用它来查找所有行: ^(?!*System.Windows.Forms).Windows.Forms$ 但我不能用替换,因为它显然删除了整行。有什么想法吗?您只需将“向后看”调整为 (?<!System\.)Windows\.Forms (? 或者更精确地说,使用单词边界: (?<!\bSystem\.)\bWindows\.Forms\b (? 见 look

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

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

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


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

您只需将“向后看”调整为

(?<!System\.)Windows\.Forms
(?
或者更精确地说,使用单词边界:

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

lookback是在当前位置遇到某些子模式之前检查文本。因此,您需要匹配和使用的文本是
Windows.Forms
,但不应在前面加上
System.
。因此,lookback(负数)只应包含
System\。
(或
\bSystem\。


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

效果很好。谢谢!