Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2008 search and replace regex

I have a large solution with a lot of lines that I need to replace. In Visual Studio, you can search and replace with the aid of regular expressions.

I want to replace lines like:

rst.Fields("CustomerName").Value
rst.Fields("Address").Value
rst.Fields("Invoice").Value

To:

row("CustomerName").ToString()
row("Address").ToString()
row("Invoice").ToString()

Thus keeping the dynamic text part, which can vary.

Is this possible and how?

Update, solution:
Search: rst.Fields{\(.*\)}\.Value
Replace: rst\1.ToString()

Thanks JaredPar!

like image 293
Michel van Engelen Avatar asked Oct 02 '10 17:10

Michel van Engelen


People also ask

How do I change the regex code in Visual Studio?

How to enable VSCode regex replace. First, you need to press Ctrl + H on Windows and Linux, or ⌥⌘F on Mac to open up search and replace tool. In order to activate regex search and replace in VSCode, you have to click on the . * button near the input.

How do I search for a regular expression in Visual Studio?

Visual Studio has a builtin helper utility to create regular expressions right from Visual Studio. Open the “Find in Files” dialog window, Select “Regular Expression” like a boss. Then, click on the button next to the “Find What” input box. And you'll see the hidden treasure.

Can I use regex in replace?

How to use RegEx with . replace in JavaScript. To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring.


1 Answers

Try the following

  • Search Expression: ASpecificCommand(\(.*\))\.ASpecificProperty
  • Replace Expression: ATotallyDifferentCommand\1.ATotallyDifferentProperty

Note: This is not a perfect solution. Since there are (s involved and hence matching of nested parens, a regex won't ever be a perfect solution. However it should get the job done for the specific pattern you posted

like image 64
JaredPar Avatar answered Nov 03 '22 01:11

JaredPar