Regular Expressions (RegEx) – Wildcard Pattern Find and Replace – Visual Studio

On a recent project where I used ILSpy and Reflector.NET to extract source code from an old DLL, I ended up with some thousands of slightly incorrect things.  So in order to make the code workable, I had to do a wildcard find of complex things and fix them using Find and Replace in Visual Studio.

I needed to do a wildcard replace of things like this:
.set_PropName(somevalue);

with a format like this:
.PropName = somevalue;

After experimenting, the following Find and Replace criteria worked.  (Make sure to select the little [.*] icon in the Find and Replace window, which tells Visual Studio 2013 or later to use regular expressions. Also, only ever just do a Find All before replacing even a single match until you tune your regex find statement exactly as you need it.)

Find box:
.set_(.[a-zA-Z0-9\-\"_]*)(\()(.[a-zA-Z0-9\-\"_]*)(\));

Replace box:
.$1 = $3;

What is all this madness?  Well, a $1, $2, $3, etc. in the Replace statement refers to each of the paren-delimited groups of info in the Find statement.  Regex find groups are delimited with ( and ).  So the first group is (.[a-zA-Z0-9\-\”_]*) and the third group is (.[a-zA-Z0-9\-\”_]*).

BEST PRACTICES:
Check code into the source repository first. Use Find All, not Replace All, in order to examine what is going to be touched by the replace operation. In the Find Results window, examine the found items one at a time for accuracy, since regular expression matching often finds more than is expected. Then replace only one or two items to see if the replace statement is also correct.

LOTS MORE EXAMPLES

To find uninitialized string variables, such as string myStringVar;, I placed this in the Find box:
string (.[a-zA-Z0-9_]*?)(\;)
To replace with initial value of string.Empty, such as string myStringVar = string.Empty;, I placed this in the Replace box:
string $1 = string.Empty;

To find unwanted extra closing paren, such as this.lblMyLabel.Text), I placed this in the Find box:
= this.lbl(.*?).Text\)
To replace, getting rid of unwanted unnecessary closing paren, such as this.lblMyLabel.Text, I placed this in the Replace box:
= this.lbl$1.Text

More examples are below. The first line goes in the Find box and the second line goes in the Replace box. I first opened open all my source files in the VS IDE or else VS Find and Replace – Entire Solution does not find everything all at once.

Change all instances of int somevariable; to int somevariable = 0;
int (.[a-zA-Z0-9_]*?)(\;)
int $1 = 0;

Change all instances of .get_SomePropName() to .SomePropName
.get_(.[a-zA-Z0-9_]*?)(\(\))
.$1

Change all instances of .ItemType == somenumber to .ItemType == (ListItemType)somenumber
.ItemType == (.[0-9]*?)
.ItemType == (ListItemType)$1

Change all instances of (SomeObject.PartialMethodNameRestOfTheMethodName to (QualifiedName.SomeObject.PartialMethodNameRestOfTheMethodName
\(SomeObject.(.[a-zA-Z0-9]*?)RestOfTheMethodName
(QualifiedName.SomeObject.$1RestOfTheMethodName

Change all instances of .get_Item(SomeKeyHere) to [SomeKeyHere]
.get_Item\((.[a-zA-Z0-9_]*?)(\))
[$1]

Change all instances of .get_Item("SomeKeyHere") to ["SomeKeyHere"]
.get_Item\((.[a-zA-Z0-9_\"]*?)(\))
[$1]

Comment out an entire paragraph of code, on multiple lines and with brackets:

To change this

IEnumerator _myvar_ref_0;
if (_myvar_ref_0 is IDisposable)
{
((IDisposable)_myvar_ref_0).Dispose();
}

Into this
// IEnumerator _myvar_ref_0; /* 2015-01 commented code */
// if (_myvar_ref_0 is IDisposable) /* 2015-01 commented code */
/*{
((IDisposable)_myvar_ref_0).Dispose();
}*/ /* 2015-01 commented code */

I created and used the following Find and Replace statements (in three sepearate Find-Replace operations)
Set 1
Find
IEnumerator _myvar(.[a-zA-Z0-9_]*?)(\;)
Replace
// IEnumerator _myvar$1; /* 2015-01 commented code */
Set 2
Find
(\*\/\r\n.[\t]*)if (\(_myvar)(.[a-zA-Z0-9_]*?)( is IDisposable\))(\r\n)
Replace
$1// if $2$3$4 /* 2015-01 commented code */$5
Set 3
Find
(\*/\r\n.[\t]*)({\r\n.[\t]*\(\(IDisposable\)_myvar)(.[a-zA-Z0-9_]*)(\)\.Dispose\(\)\;\r\n.[\t]*\})
Replace
$1/*$2$3$4*/ /* 2015-01 commented code */

Problem: Internet Explorer – “setup_exe could not be downloaded” – (with/without ClickOnce installs)

When I was attempting to run an installation from a secure (https) web site, despite having Enhanced Security Configuration disabled, despite having the site in my Trusted Sites zone, I still received the aforementioned error.  I did the following to fix it:

Clicked the gear toolbar icon in the upper right corner of IE or click the Options menu.
Clicked the Internet Options menu item.
Clicked the Advanced tab.
Scrolled down and cleared/deselected the “Do not save encrypted pages to disk” checkbox.
Refreshed the page and tried again. I then saw “Do you want to run or save setup.exe (nnnn KB) from website.xyz?” The Run and Save buttons were finally available and the filename was setup.exe instead of setup_exe.

This error can occur anytime you try to download an executable. I came across it trying to do a ClickOnce installation from an HTTPS site.

Also, sometimes the ClickOnce installation will begin but it will fail when it itself tries to download additional components and resources (aka the “An error occurred while downloading a required file” error).

Hello world!

Finally this blog is off the ground!  Please note the tabs above, areas that will gradually be filled with resources and discussion.