33

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;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joseph Le Brech
  • 6,541
  • 11
  • 49
  • 84

6 Answers6

83

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.

Oliver
  • 11,297
  • 18
  • 71
  • 121
SoManyGoblins
  • 5,605
  • 8
  • 45
  • 65
  • 2
    I was about to draw the conclusion that backreferences had been removed from Visual Studio 2012 until I read this answer. It is not mentioned at all in the official documentation http://msdn.microsoft.com/en-us/library/2k3te2cs(v=vs.110).aspx – Klas Mellbourn Oct 12 '12 at 12:40
  • 1
    Just to elaborate with an example for VS 2012, a reg. exp. for removing the surrounding quotes from text elements in an XML document: _Find box_: **>"(.*)"<** _Replace box_: **>$1<** – Ed Graham Dec 20 '12 at 15:09
  • 2
    Huzzah for proper regular expressions in VS 2012! This is huge! – Cristian Diaconescu Apr 05 '13 at 11:24
  • Used for find-replace in VS2019 and it worked just fine! – A. J. Mar 15 '21 at 14:07
33

In Visual Studio 2010 and earlier, use regular expressions with back references

Visual Studio's regular expressions are completely different from what I've learned. Took me some time to figure out the correct answer.

Search for

{foo}{:d+} = \1\2

Replace with

\1\2 = bar\2

Back references are done by tagging with curly braces {foo}. :d+ is the same for \d+

Read more about VS RegEx here

danio
  • 8,548
  • 6
  • 47
  • 55
Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
3

Here it is, type exactly as it is displayed here

Search: (\w+\d+\s*=\s*)[^\d]+(\d+);

Replace: $1bar$2;


Read more: Using Regular Expressions in Visual Studio

BJovke
  • 1,737
  • 15
  • 18
3

As simple as this in Visual Studio 2019 Find/Replace. I needed to replace FORTRAN IO format string to C++ format and used sub-expression and numbers of regexp.

Example: find: "f9.8", "f18.3", in line and replace with %9.8f, %18.3f
reg exp: Find = ( f)(\d+.\d+) Replace = %$2f

userom
  • 403
  • 5
  • 8
3

I can be done without a regular expression as well:

Replace = foo with = bar.

If a regular expression is needed, one could use:

foo(\d*) = foo(\d*);

Replace with:

foo\1 = bar\2;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jayendra
  • 52,349
  • 4
  • 80
  • 90
  • This wouldn't actually work because `\d` is not an accepted character in Visual Studio. – Ranhiru Jude Cooray Oct 22 '11 at 06:29
  • 3
    Backreferences for replacing need to use `$1`, not `\1`, in vs 2012 at least. – mbomb007 Apr 08 '15 at 18:51
  • This is for older, pre-2012 versions of Visual Studio. For newer versions see the answer for this question: https://stackoverflow.com/questions/3147096/visual-studio-find-and-replace-regex/3147177#3147177 – paulwesterberg Jul 27 '17 at 16:20
-2

If you would be more variable:

Regex.Replace(input, @"(?<=\= )[^;0-9]*(?=[0-9]*;)", replacewith);

This search for = and (anynumber); and replace that between.

Edit: The number is optional.

San
  • 868
  • 1
  • 9
  • 18