【发布时间】:2018-02-07 07:05:02
【问题描述】:
我正在尝试编写一个正则表达式来查找和替换 Visual Studio 中的代码。
以下是现有代码示例:
GameObject go = Instantiate(...);
GameObject go2 = (GameObject)Instantiate(...);
GameObject go3 = ReplaceThis.Instantiate(...);
GameObject go4 = DontReplaceThis.Instantiate(...);
我基本上需要一个正则表达式来将 sn-p 更改为以下内容:
GameObject go = ReplacedClass.Instantiate(...);
GameObject go2 = (GameObject)ReplacedClass.Instantiate(...);
GameObject go3 = ReplacedClass.Instantiate(...);
GameObject go4 = DontReplaceThis.Instantiate(...);
如您所见,DontReplaceThis.Instantiate(...); 是唯一未被替换的。
到目前为止我所拥有的:
Find: (?=.*Instantiate)(?=?!DontReplaceThis.)
直到我添加了?= 逻辑和正则表达式。
编辑:
本质上,我希望有一个正则表达式查找和替换任何匹配(.*Instantiate) 但在类范围的运算符成员之前不包含DontReplace 的内容。它还应该将替换字符串附加到.Instantiate 的所有实例,使它们变为ReplacedClass.Instantiate
【问题讨论】:
-
这很难理解,你能不能也把一些输入文本的例子放在引号和你想要的输出中
-
@MichaelRandall 我在我的问题中添加了对匹配条件的解释。至于输入,您可以使用第一个代码 sn-p,其输出将是第二个代码 sn-p。我不确定如何简化示例输入和输出。
-
啊,我明白了,你在某种源文件上运行它
-
@ChristopherLeong 请考虑accepting/upvoting the answer then。
标签: c# regex visual-studio-2015 replace find