EVERY-DAY REGEX

You may not like regular expressions. If so, you’re not alone. Many developers avoid RegEx like the plague. I don’t blame them; regular expressions can be very cryptic and confusing, especially to decipher. While they can easily be abused, I believe that using regular expressions in your software does have an appropriate place. RegEx can be a powerful way to add configurability to applications. The problem, however, is that RegEx has a fairly steep learning curve, so, in the opinion of many developers, it’s not worth it to learn RegEx just for the few instances where it really makes sense to use it.

What if there was an every-day use for RegEx that, if you invested a little time to learn it, would save you a fair amount of time? What if this every-day use for RegEx not only saved you time, but also transformed the most menial and monotonous tasks into something fun and mentally stimulating? If such a thing did exist, it would not only make you happier and more productive, but it would give you constant practice with RegEx so that, when the time came, you could use RegEx in your applications without the hurdle of a learning curve. As you’ve probably guessed, such a use for RegEx does exist, and it may be just useful enough to change your opinion.

The tool that I’m referring to is the RegEx search-and-replace feature in Visual Studio. Why is it so useful, you ask? Well, perhaps an example is the best way to illustrate it. Let’s say that you have a list of C# variables like this:

StudentBusiness studentBusiness
ClassroomBusiness classroomBusiness
ScheduleBusiness scheduleBusiness
ReportWriter reportWriter

And you want to reformat it to look like this:

StudentBusiness _studentBusiness = new StudentBusiness();
ClassroomBusiness _classroomBusiness = new ClassroomBusiness();
ScheduleBusiness _scheduleBusiness = new ScheduleBusiness();
ReportWriter _reportWriter = new ReportWriter();

You could, of course, make the changes manually by typing all of those underscores, equal signs, and new keywords; copying and pasting the type name on each line; and copying and pasting the parentheses and semicolons at the end of each line. But that’s a bit tedious and mundane.
Alternatively, you could perform the following RegEx search-and-replace operation on those four lines:

  • Search: (w+) (w+)
  • Replace: $1 _$2 = new $1();

In this simple example, it probably doesn’t make much of a difference. It probably would have taken you just ask long to write the regular expression as it would have for you to just manually reformat the four lines of code. However, if there were more lines of code involved, you could probably see how it would be significantly easier to use RegEx. However, even in a simple example like this, where it may not save you any time, it’s at least more interesting and gives you more experience with RegEx. So, what’s to lose?

How about another example? Let’s say that you want to find all of the places in your VB.NET application where you call String.Compare passing StringComparison.InvariantCultureIgnoreCase as the third argument. For instance, you want to find lines of code like this one:

Dim result As Integer = String.Compare(student1Name, student2Name, StringComparison.InvariantCultureIgnoreCase)

You could right-click on InvariantCultureIgnoreCase and select Find All References, but then you’d find every line where that value is used (e.g. String.Equals, String.IndexOf). On the other hand, you could Find All References of the Compare method, but that would show you everywhere that it’s called with other comparison options. What you could do, though, is search the entire project for all occurrences of the following RegEx:

  • Search: String.Compare(([^,]+, ){2}.*InvariantCultureIgnoreCase

That will limit the results to only the exact lines that you care to find. Again, if you were only calling that String.Compare method in a few places, the Find All References option would be quicker, but if you were calling the method in hundreds of places, RegEx would be much easier. These are simple examples to illustrate its functionality. They aren’t necessarily the most practical examples, but I’m sure you can imagine ways in which functionality like this would be useful. As you use RegEx searches on a regular basis, you will discover more and more useful purposes for it.

So how do you use RegEx in to search in Visual Studio? It’s really easy, actually. When you use the standard Quick Find feature, you just need to select the Use Regular Expressions option. The same option is on the Quick Replace window. There is also a similar option in the Find in Files and Replace in Files windows. Not only are regular expressions supported throughout all of Visual Studio’s search and replace features, it is also available in some of Microsoft’s other products, such as SQL Server Management Studio.

Every-Day RegEx Illustration

If you aren’t familiar with RegEx, don’t be afraid. It’s not nearly as daunting as it looks. Think of it as basically a more advanced way of supporting wildcards in your searches (similar to * or ? in a command line). Regular expressions are easier to construct than they are to decipher. Since Visual Studio dynamically highlights the matches in your code as you type the regular expression, it’s easy to tell when you’ve made a mistake. There are many RegEx tutorials for beginners that are available online. Beware, however, that RegEx syntax can vary depending on the technology. Java’s RegEx implementation is not exactly the same as .NET’s implementation. Therefore, to avoid confusion, it’s best to find a tutorial geared specifically for .NET. MSDN’s introduction to RegEx is a good place to start:

https://msdn.microsoft.com/en-us/library/hs600312(v=vs.110).aspx

Once you get the gist of it, a simple quick-reference sheet, like this one on MSDN, is also very helpful:

https://msdn.microsoft.com/en-us/library/az24scfc.aspx


#AppDev
#SoftwareDev
#Developers

Share This:
Facebooktwitterredditpinterestlinkedintumblr

Steven is a senior software engineer with experience developing stable, mission-critical applications for the public-safety market since 1998. He has been awarded the highest level as a Trusted User by his peers on StackOverflow. He is the author of Herding Ocelots, a blog aimed at providing help to software development managers.

Posted in Instructional Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*