Overview Of Regular Expressions With Visual Studio

These days, having knowledge of Regular Expressions can make you stand out in a crowd of normal developers. With every day’s work to be done, it’s really hard to find time to learn everything. But Regular Expressions are something that developers use for their day-today work. Those who are new or are not familiar with RegEx (Regular Expressions), are usually dependent on searching for required regex on the internet, in places like  StackOverflow and other sites. Most importantly, this is a skill which can be used across platforms.

In this post, you’ll learn how to create regular expressions by yourself and how to start practicing while working with Visual Studio. Consider that  you’re working on a large code base and you regularly need to search for method names, event names or just some code lines or anything that you want to look up in the code base. Usually, what developers do use is “Find in Files” or “Find All” with given terms. But what if you don’t know the exact name of the code line you want to search? What will you do? Well, you can definitely think of a pattern that may help you find your target line of code.

I have this EntityFramework 6 code base forked from Git repository. This is a pretty large code base to look into something. So for example, I want to look for all classes used in validation. I’ll start my search with the following in mind.

  • Assuming they’re named properly with the prefix, suffix, or in between the name of classes.
  • The class names may contain variations like “Validator”, “Validation”, Validatable” etc.

Either I can search for all these terms individually and collect results manually, or I can use pattern matching and find all results in one go. So, I’m going to open the “Find in files” window (SHIFT + CTRL + F) and start writing a Regular Expression for it.



I’m using the expression .* class \w+Valida\w+ (I’ll explain next what it means) to find a pattern based on my assumptions above. After clicking on “Find All”, I have the following results.



As you can see, all my desired classes are listed in the results. “Voila!!”.

Instead of searching for individual keywords, I have my result in single search. I saved time and I have my regular expression which might not be perfect but it did the job.

Explanation of Regex used above

Let's break this down and see what individual things means -

  • .
    Represents anything (character, non character, symbols, digits etc.) in a string.

  • *
    Represents 0 or more occurrences.

  • class
    Represents a string literal and it will be an exact match.

  • \w
    Represents any alphanumeric, including _ (underscore).

  • +
    Represents 1 or more occurrences.

  • Valida
    Represents a string literal and it will be an exact match.

  • \w+
    As explained above.

Well, was this easy or was it not?

Another simplest case I can mention here which every developer might have faced is managing Spaces in Code --  in Text or in Strings at run time. A simple way to remove additional spaces using regular expression is (I’m using C# Regex library for this demo):

Regex.Replace(“ Some Text with un even spaces”, “(\s+)”, “(\s)”).Trim();

Output

“Some Text with uneven spaces”

Let’s go back a little and start with basics and then go step by step

Step 1

If you are completely new to Regular Expressions, then you can start with a free practice website which provides insights and great details about each exercise.

Explanation of symbols is provided in the right side column but I’m also going to use the screenshot from the website. This will give you a pretty good explanation of each special character and symbol used in Regular Expressions.

After memorizing each and every one sof these notations, you can start doing the exercises one by one. And in barely 12 hours of practice, you will be able to write you first Regular Expression without any help.

Another source to learn and test the regex that I prefer is  https//regex101.com

This site provides better insights of your regular expression on the fly. But I would say, once you are on the next level and have confidence, go to the website and challenge your Regex with various tests and insights including optimization of your Regular Expression.

Images

Step 2

Now that you have learned to write the basic regular expression, it’s time to test your skills in Visual Studio. As I stated in the example above, you can start using Regular Expression as a search option instead of searching for “Contains” in Visual Studio. And at this step, you have already stepped into the advanced user of Visual Studio.

Step 3

Create complex expressions and get help right from visual studio. Visual Studio has a builtin helper utility to create regular expressions right from Visual Studio.

Open the “Find in Files” dialog window, Select “Regular Expression” like a boss.

Images

Then, click on the button next to the “Find What” input box. And you’ll see the hidden treasure.

Images

These are the tricks at your finger tips. Now, you can start writing and testing your regex using “Quick Find” as well. It shows you the live match results by highlighting the matching result within the Editor window.
So for example, if I want to search anything that starts with Collection, then here’s my quick tip. Just go to “Quick Find’'” (CTRL + F) and write your regex,

Images

Images

With the combination of search and replace, you can easily refactor your code without repeating steps and save a lot of time for other stuff.

Testing your Random Regular Expression

You can again use the Visual Studio editor to test your regex which you might be going to use in your code. How? Simply create a text file with your sample Text data. E.g. - you want to capture cell phone number from a Text.
  1. /* == Matching cell phone numbers == */  
  2. 3473459090  
  3. 12123232111  
  4. ABdfasdfasdlf  
  5. Another test string  
  6. 1106 is a zip code  
  7. but this is a cell phone number + 1 121 212 2241  
  8. 121 212 2241  
Now, open the "quick find" box and put your Regular Expression in there to see what is matching and what is not.

For e.g. I prepared a Regex to find the phone numbers as 1?[\s]?\(?(\d{3})\)?[\s]?\d{3}[\s]?\d{4}

Let’s see how my results are looking.

Images

Now, instead of going to some online website to test your regex, you can give a dry run directly within Visual Studio. I hope you have had fun reading and learning one of the essential skills of quick and smart developers --  which could be you. Keep practicing.


Similar Articles