简体   繁体   中英

Visual Studio 2015 community - Regex Find and Replace

I'm trying to write a regex expression to find and replace code within 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(...); 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.

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. It should also append the replacement string to all instances of .Instantiate such that they become 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
    • \\. - a . char
  • (?<!\\bDontReplaceThis\\.[\\w.]*) - a negative lookbehind that fails the match if there is DontReplaceThis. as a whole word ( \\b is a word boundary) followed with 0+ word or . 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.]* )
  • (\\bInstantiate) - Group 1 ( $1 ) that matches Instantiate
  • (?=\\() - 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. 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 )

((\\w+\\.)|\\s)? matches optionaly if preceded by either

\\w+\\. any word character occuring 1 or more times followed by a dot

| OR

\\s any white space character

\\( 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)

For more information have a look on regex look-arounds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM