288
votes

I realize there are a ton of questions about this, but none that I found specifically referenced which VS version they referred to. With that important information lacking, I still was unable to successfully use the answers I found. The most common was

  • Surround with {}, display capture with \1, \2, \n

However, that seems to be the old method of doing regex find and replace in Visual Studio, and it does not work in VS 2012.

4
This is not too hard to find in the help: in the Search/Replace dialogue, F1; early in Finding and Replacing Text there is a link to Using Regular Expressions in VS; there the 2nd tip refers you to Substitutions in Regular Expressions; there it gives you the basics and says there is more detail under Substituting a Numbered Group and Substituting a Named Group.PJTraill
@PJTraill it's nice to know that they made it easier to answer a question for their 2012 version sometime between 2013 and 2015. By the votes though, I think it's fairly obvious that it's still not "not too hard" to find in the help. IMO: If it's easier to search and vote on a stack overflow article than to use the find, replace, capture without looking up help at all, then it's a lot harder than it should be.SgtPooki
One thing that bugs me about MSVS 2012/3 is the two different Search/Replace dialogues, in particular the smaller (Ctrl-H) one, where I keep accidentally (with keystrokes probably meaning something else somewhere else) altering the scope of the search. But the point I want to make is that the larger (Ctrl-Shift-H) dialogue has buttons after the Find and Replace fields that give you a list of the more important possibilities, including what you are looking for – so you don’t need Help! I guess that many people either don’t spot the buttons or land in the Ctrl-H dialogue and can’t see them.PJTraill
I agree that the help is bit of a bother to find; I suspect they give priority to plain text searching. My point is more that if one knows or assumes it must be there one can find it reasonably easily; all to often I follow that route, as I prefer to use the canonical documentation once I can find it. I didn’t mean to sound disparaging; I reckon it is both “too hard” and “not too hard” — they are not opposites if you see “not too” as qualifying “hard” and add in the answer to the implicit “hard for what?”.PJTraill
I "knew" that REGEXP tagging was using '()', Re read the documentation, and it didn't work... so I came here to make sure... then I did some more experimentation, and it worked. So IMHO the MSDN documentation needs to be super clear that () are used to "tag" an expression, for those of us who had to learn the old VS6 "tag" nomenclature. It would be nice if they could use that same nomenclature. Numbered groups... just doesn't fire up the synapses well enough IMHO.Ross Youngblood

4 Answers

391
votes

To find and replace in VS 2012 and VS 2015 you do the following:

Example (thanks to syonip)

In the find options, make sure 'use regular expressions' is checked, and put the following as the text to find:

_platformActions.InstallApp\((.+)\)

And the following as the text to replace it with:

this.Platform().App($1).Install()

Note: As SLaks points out in a comment below, the change in regex syntax is due to VS2012 switching to the standard .Net regex engine.

Note: Another commenter pointed out that this works in Visual Studio Code (vscode) as well

18
votes

To add an example of this, here is something I had to do in my code:

Find what:

_platformActions.InstallApp\((.+)\)

Replace with:

this.Platform().App($1).Install()

This replaces any call to InstallApp(x), with this.Platform().App(x).Install().

*Don't forget to mark "Use regular expressions" in Find options

14
votes

If you want to work with using group names (using the same sample as above):

Find what:

_platformActions\.InstallApp\((?<mygroupname>.+)\)

Replace with:

this.Platform().App(${mygroupname}).Install()
13
votes

To improve the above answers: You should replace

_platformActions.InstallApp\((.+)\)

with

this.Platform().App(${1}).Install()

Mind the ${1} if you ever want to add a number behind the capture. $18 will try to insert the 18th search capture, not the first with an 8 appended.