我正在使用 VS 正则表达式尝试删除字符串 Windows.Forms 但不删除 System.Windows.Forms
我可以用它来找到所有的行:
^(?!.*System.Windows.Forms).Windows.Forms.$
但我不能使用替换,因为它显然会删除整行。有什么想法吗?
最佳答案
你只需要将lookbehind调整为
(?<!System\.)Windows\.Forms
或者更准确地说,使用单词边界:
(?<!\bSystem\.)\bWindows\.Forms\b
参见 regex demo
lookbehind 正在检查文本 before 当前位置遇到一些子模式。因此,您需要匹配和使用的文本是 Windows.Forms
,但不应以 System.
开头。因此,lookbehind(负的)应该只包含 System\.
(或 \bSystem\.
)。
请注意,必须对点进行转义以匹配文字点。
关于c# - VS 2015 查找并替换这个字符串而不是那个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35615227/