How do you reference a capture group with regex find and replace in Visual Studio 2012, 2013, 2015, and VS Code
To find and replace in VS 2012 and VS 2015 you do the following:
- Surround with (), display capture with $1, $2, $n
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
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
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()