One of the latest neat little features I’ve started using is Visual Studio’s Find & Replace function but using regular expressions for the search terms, for example:
Find what:
\.Item\("{(:a|_|-)*}"\)
Replace With:
!\1
Converts
.Item("cState")
Into:
!cState
If we were to break down what each regex is doing we would have the following:
In our first expression we have
\.Item\("{(:a|_|-)*}"\)
- The
:a
is a VS shortcut for[a-zA-Z0-9]
(:a|_|-)
groups it with the underscore and dash giving us the effect of ([a-zA-Z0-9]|_|-)*
matches 0 or more of the previous expression
Now the special chars:
- the
{}
tags the expression so we can use it as a variable in our replace box. - the replace box just has
!\1
, where\1
refers to the first tagged expression in our find box, if we had multiple{}
in the find string, we could address them accordingly with\2
,\3
and so on.
Here’s a second example using multiple tagged expressions. Another time I was refactoring a sub routine into a function. Previously the sub was taking in a datatable by reference and modifying it locally, but now it was returning the datatable. So every where the method was being called needed to be altered in pretty much the same way. The following terms made the rest of my refactoring chore much easier:
Find What:
Code\.FillDataTable\({"(:a|_|-)*"}, {(:a|_|-)*}\)
Replace With:
\2 = FillDataTable(\1)

Takes code of the format:
Code.FillDataTable("usp_FillStates", dtStates)
and changes it to:
dtStates = FillDataTable("usp_FillStates")
Thanks to the find/replace feature this saved me a bunch of time!