Visual Studio 'Find and Surround With' ins

2019-08-03 04:38发布

So i want to surround all string literals in our C++ source with an _T(...) for our unicode port.

This questions answers how I search for string literals but is there some way of surrounding the matched text with _T() instead of replacing with something else?

I intend to do it one string at a time anyway and not all at once but want to avoid having to type it out or use "Surround With" from Visual Assist myself for each string.

2条回答
地球回转人心会变
2楼-- · 2019-08-03 05:28

Jochen Kalmbach's answer might work in older versions of Visual Studio, but it didn't work for me in Visual Studio 2013. However, the small RegEx shortcut buttons to the right of the Find/Replace input boxes helped a lot:

RegEx shortcut buttons are helpful for non-RegEx experts

In Find, select the ":q Quoted string" option. In Replace, select the "$1 Substitute the substring matched by captured group number 1", and then surround $1 with _T().

Final Output

Find: ((\".+?\")|('.+?'))

Replace: _T($1)

Note that the $1 represents the RegEx expression group enclosed in the outermost parentheses.

Here's another example:

Requirement

Find:

Converter.toCustomObject($find("Anything"));

Replace (different Converter method and add parameter after $find() parameter):

Converter.toDifferentObject($find("Anything"), true);

Solution

Find (use RegEx in Find options):

Converter\.toCustomObject\((\$find\(.*)\);

Replace:

Converter.toDifferentObject($1, true);

Notice that the Replace value doesn't need to escape special characters, though you can apply some RegEx, e.g. to add a Line Break after the output, you can use this for Replace:

Converter.toDifferentObject($1, true);\r\n
查看更多
干净又极端
3楼-- · 2019-08-03 05:33

Goto: Edit|Find and Replace...|Quick Replace.. Then enter:

Find: :q
Replace with : _T(\0)
Use: Regular Expressions
查看更多
登录 后发表回答