Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search and replace in Visual Studio

In Visual Studio, when I search within a selection, I want to replace the first instance (or second, third, etc.) of a match per line using regular expressions. How would I do this?

Search and replace

foo1 = foo1; foo2 = foo2; ... foo20 = foo20; 

into the following.

foo1 = bar1; foo2 = bar2; ... foo20 = bar20; 
like image 703
Joseph Le Brech Avatar asked Oct 21 '11 10:10

Joseph Le Brech


People also ask

How do you replace multiple lines in Visual Studio?

CHANGE: The default keyboard shortcut for “Multiline Find/Replace” was Ctrl+Alt+F but it was not applied because this shortcut was already taken by “F# Interactive”. So, the new default shortcut is Ctrl+M, Ctrl+F.


1 Answers

In Visual Studio 2012, capture groups and backreferences are used just like in C#. You can capture them with common parenthesis, and backreference them with $0, $1, etc. Hope it helps!

Note that the syntax $1 is used for find-replace, but \1 is used for backreferences in the search string.

like image 52
So Many Goblins Avatar answered Sep 19 '22 01:09

So Many Goblins