331

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.

Adam Lydick
  • 1,092
  • 7
  • 15
SgtPooki
  • 11,012
  • 5
  • 37
  • 46
  • 2
    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 Aug 27 '15 at 23:21
  • 4
    @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 Jan 12 '16 at 20:40
  • 1
    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 Jan 13 '16 at 10:42
  • 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 Jan 13 '16 at 10:52
  • 1
    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 May 16 '18 at 18:12
  • Somehow, in Visual Studio 2015, I've managed to have a replace REGEX using the ${myReference} or the $n syntax not do anything for Replace All but work as expected for Replace Next. This doesn't happen for all REGEX I've tried however. – Matt Arnold Apr 02 '19 at 16:45

5 Answers5

451

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

SgtPooki
  • 11,012
  • 5
  • 37
  • 46
20

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

syonip
  • 2,762
  • 25
  • 27
17

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()
Christian Sirolli
  • 246
  • 1
  • 6
  • 18
Yepeekai
  • 2,545
  • 29
  • 22
17

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.

Luc Bloom
  • 1,120
  • 12
  • 18
1

In VSCode v(1.61.1) in Ubuntu
I have to place (*someExpression*) in like $1
So the moment is I wasn't need to use extra brackets like ($1)

Damir Nafikov
  • 162
  • 1
  • 9