简体   繁体   中英

Find and Replace All But Text Between Double Quotes in VS2010

Using Quick Replace in Visual Studio 2010, how can I replace all but text surrounded by double quotes. I am not sure if I could use WildCards or Regex to do this. An example is below.

Here is the original code:

TypeByName("bPhone3", "9999");
TypeById("bFirstName", "Don");

and I am wanting to replace the text with something like this:

Type("bPhone3", "9999", Selector.Name);
Type("bFirstName", "Don", Selector.Id);

I am doing this for several hundred changes, so Quick Replace is my only real choice right now. I need to find a way to keep anything in quotes and change the text around it.

So I was tinkering with Visual Studio's find and replace options and this is actually possible using RegEx and tagged expressions. I used this and it works for the strings you listed.

Find What: TypeBy{(.*)}\\({"[^"]*"}, {"[^"]*"}\\);

Replace with: Type(\\2, \\3, Selector.\\1);

Remember to tag 'use regular expressions' and you should be golden

More information on tagged expressions can be found here

Edit: updated a bit as I noticed you have both name and ID, may need to make a few other changes depending on other small quirks in what you want to change.

You can do this with regular expressions in Visual Studio find and replace:

Find: TypeByName\\((\\".+?\\"),\\ (\\".+?\\")\\);
Replace: Type($1, $2, Selector.Name);

Which would turn
TypeByName("bPhone3", "9999"); into
Type("bPhone3", "9999", Selector.Name);

Edit
you can also do this:

Find: TypeBy(.+)\\((\\".+?\\"),\\ (\\".+?\\")\\);
Replace: Type($2, $3, Selector.$1);

Which would turn
TypeByName("bPhone3", "9999"); into
Type("bPhone3", "9999", Selector.Name);
AND
TypeById("bFirstName", "Don"); into
Type("bFirstName", "Don", Selector.Id);

It basically turns TypeBy{1}({2}, {3}) into Type({2}, {3}, Selector.{1})

NOTE THAT I TESTED THIS IN VS2012, AND IT COULD BE DIFFERENT FOR VS2010

maybe you're looking for a refactoring engine where you can right click on a symbol, and then go to refactor and then rename

for your particular solution, you can also start with refactoring, and then toying around with default parameters

public void Type(string str1, string str2, string str3 = "Default String")
{ 
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM