[英]Visual Studio 2015 community - Regex Find and Replace
I'm trying to write a regex expression to find and replace code within visual studio. 我正在尝试编写一个正则表达式来查找和替换Visual Studio中的代码。
Here are samples of existing code: 以下是现有代码的示例:
GameObject go = Instantiate(...);
GameObject go2 = (GameObject)Instantiate(...);
GameObject go3 = ReplaceThis.Instantiate(...);
GameObject go4 = DontReplaceThis.Instantiate(...);
I basically need a regex expression to change the snippet to the following: 我基本上需要一个正则表达式来将代码段更改为以下内容:
GameObject go = ReplacedClass.Instantiate(...);
GameObject go2 = (GameObject)ReplacedClass.Instantiate(...);
GameObject go3 = ReplacedClass.Instantiate(...);
GameObject go4 = DontReplaceThis.Instantiate(...);
As you can see the DontReplaceThis.Instantiate(...);
如您所见,
DontReplaceThis.Instantiate(...);
is the only one that wasnt replaced. 是唯一没有被替换的。
So far what I have: 到目前为止,我有:
Find: (?=.*Instantiate)(?=?!DontReplaceThis.)
This worked up until the point I added the ?=
logical and for the regex expression. 直到我添加
?=
逻辑和regex表达式为止。
Edit: 编辑:
In essence I wish to have a regex expression Find and Replace anything that matches (.*Instantiate)
but does not contain DontReplace
before the Member Of Operator for the class scope. 本质上,我希望有一个正则表达式可以查找并替换与
(.*Instantiate)
匹配但在类作用域的Member Of Operator之前不包含DontReplace
的所有内容。 It should also append the replacement string to all instances of .Instantiate
such that they become ReplacedClass.Instantiate
它还应将替换字符串附加到
.Instantiate
所有实例上, .Instantiate
使它们成为ReplacedClass.Instantiate
You may use 您可以使用
(?:\w+\.)?(?<!\bDontReplaceThis\.[\w.]*)(\bInstantiate)(?=\()
See the regex demo 见正则表达式演示
Details 细节
(?:\\w+\\.)?
- an optional (as (...)
, eg, should not be matched) sequence of (...)
,例如不应匹配)序列
\\w+
- 1 or more word chars \\w+
-1个或多个字字符 \\.
- a .
.
char (?<!\\bDontReplaceThis\\.[\\w.]*)
- a negative lookbehind that fails the match if there is DontReplaceThis.
(?<!\\bDontReplaceThis\\.[\\w.]*)
-如果没有DontReplaceThis.
则负向后DontReplaceThis.
匹配失败DontReplaceThis.
as a whole word ( \\b
is a word boundary) followed with 0+ word or .
\\b
是单词边界),后跟0+单词或.
chars immediately to the left of the current location (effectively, it fails matches like var x = DontReplaceThis.Some.Class.Instantiate(...)
, if you need to match it, you will need to remove [\\w.]*
) var x = DontReplaceThis.Some.Class.Instantiate(...)
,如果您需要匹配它,则需要删除[\\w.]*
) (\\bInstantiate)
- Group 1 ( $1
) that matches Instantiate
(\\bInstantiate)
-与Instantiate
匹配的组1( $1
) (?=\\()
- followed with a (
char (not added to the match since it is a positive lookahead). (?=\\()
-后跟一个(
字符(由于正先行而未添加到匹配中)。 Results: 结果:
Since you are looking for Instantiate
as a method call and only want to replace it if it is not preceded by DontReplaceThis.
由于您正在寻找
Instantiate
作为方法调用,并且只希望在DontReplaceThis.
之前没有它的情况下将其替换DontReplaceThis.
You can use negative lookbehind: 您可以在后面使用否定式:
Searchpattern: 搜索模式:
((\w+\.)|\s)?(?<!DontReplaceThis\.)(Instantiate\()
Replacement pattern: 更换方式:
ReplacedClass.$3
Explanation: 说明:
?<!
match only if not preceded by the following string. 仅在没有以下字符串的情况下匹配。 (in this case:
DontReplaceThis
) (在这种情况下:
DontReplaceThis
)
((\\w+\\.)|\\s)?
matches optionaly if preceded by either 如果前面有任一个,则匹配optionaly
\\w+\\.
any word character occuring 1 or more times followed by a dot 任何出现1次或多次并后跟一个点的单词字符
|
OR 要么
\\s
any white space character \\s
任何空格字符
\\(
match opening parentheses. Indication of a method call \\(
匹配括号。方法调用的指示
(Instantiate\\()
the parentheses defince a group which can be accessed in the replacement pattern by $3
(inserts the third matched group) (Instantiate\\()
括号定义了一个可以用$3
替换模式访问的组(插入第三个匹配的组)
For more information have a look on regex look-arounds 有关更多信息,请查看正则表达式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.