I found out that you can use Regex for search queries in Find and Replace in Visual Studio. I have a lot of similar lines like so:
datum["Id"] = id; datum["Name"] = name;
I also have more lines like so:
this.Id = datum["Id"]; this.Name = datum["Name"];
I want to turn the first lines to:
datum.Set("Id", id); datum.Set("Name", name);
And the second set of lines to:
this.Id = datum.Get<int>("Id"); this.Name = datum.Get<int>("Name");
How is it possible to do with Find and Replace and Regex? I can't figure it out.
After enabling regular expression by clicking on 'use Regular Expression' button, Place following in find:
datum\\[\\"Id\\"\\]\\s*=\\s*id\\;
and place following in replace (no need to use regex in replace):
datum.Set("Id", id);
Similarly:
In Find:
datum\[\"Name\"\]\s*=\s*name\;
In replace:
datum.Set("Name", name);
In Find:
this\.Id\s*=\s*datum\[\"Id\"\]\;
In Replace:
this.Id = datum.Get<int>("Id");
In Find:
this\.Name\s*=\s*datum\[\"Name\"\]\;
In Replace:
this.Name = datum.Get<int>("Name");
I think you need universal Regex with substitutions in replace field.
This expression:
([a-zA-Z0-9_]+)\["([a-zA-Z0-9_]+)"\] = ([a-zA-Z0-9_]+);
matches your first lines.
You should input that in "Find..." field.
This expression allows you to make replacements you need:
$1.Set("$2", $3);
Put it to the "Replace..." field.
Numbered expressions like this $n
in replacement targets to groups in original Regex that defined by (...)
and numbered in sequential order.
So, replacement for your second lines would be like that:
Find: (this.[a-zA-Z0-9_]+) = ([a-zA-Z0-9_]+)\\["([a-zA-Z0-9_]+)"\\];
Replacement: $1 = $2.Get<int>("$3");
You could find more information on MSDN
PS Feel free to ask more question for details.
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.