开发者

Search and replace with regular expressions under Visual Studio 2003

I have a large C++ code base that I'm doing some refactoring on where a number of functions have become redundant, and hence should be removed. So I would like to replace

MyFunc(Param)

with

Param

where Param could be a literal value, variable, function call etc... From the online help I gathered that the search parameters should be

MyFunc/({+}/(

and the replace parameters simply

/1

But this gives me a syntax error in my pattern. I'm new to search and replace with regex under visual studio. Can the above be easily achieved? I've had a look at similar questions on this site, which suggest I'm roughly on the right track, but seem to be missing something.

Edit: If you can answer the above, how about if it is part of a class deference, e.g.

MyClass.MyFunc(Param) 

or

MyClass->MyFunc(Param)

(FWIW, I also picked up a copy of VisualAssist in the hope it could do this but it doesn't appear to be able to handle this situation).

Second edit: Thanks to Joe for the correct response, but for anyone else using this approach, beware of some pitfalls,

MyFunc(MyArray[MyOtherFunc(x)])

ends up as

MyArray[MyOtherFunc(x])

and

MyFunc((SomeType)x)

ends up as

(SomeTypex)

Once you do a search to check what you get prior to doing a search and replace, make sure you keep modified files open in case 开发者_如何学运维you need to undo, and backup your source files before starting, this works well enough. Even with the pitfalls listed, still a huge time saver.


Try this instead:

Find = MyFunc\({[^\)]*}\) Replace = \1

Your slashes are the wrong way around and the expression in the parenthesis ({+}) is invalid.

This won't work for parameters that contain function calls or other uses of parentheses - the balanced bracket matching problem isn't solveable using regular expressions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜