Beginner’s tips on VS Find and Replace using Regular Expressions – Tutorial #2


In my last tutorial, I talked about some basic fundamentals.  Let’s move on to narrowing down search scope some more.  Recall that previously, I had introduced:


< – Matches beginning of a word


> – Matches end of a word


Now, let’s introduce a few more!


^ – Matches beginning of a line


$ – Matches end of a line


^n – Matches n occurrences


For this tutorial, I will be using the following text lines


Hello world!


How are you today?


I’m fine, thanks!


If we want matches to return words that begin with H, we can search with “<H[a-z]*”  This will match the following results (match highlighted in blue)


Hello world!


How are you today?


I’m fine, thanks!


What if you only want to find words that begin with H and has a total of 3 characters?  You can search with “<H[a-z]^2>”.  This matches a word that begins with H, followed by two letters in the range of a-z, and then the word ends.  This match will return the word “How”.  Note that if you didn’t include the end of word match “>” and just searched with “<H[a-z]^2”, you will return the following matches:


Hello world!


How are you today?


I’m fine, thanks!


This is because we didn’t specify that the word had to end, so any word that begins with H and then had two characters in the range of a-z will match.


As we searched for beginning of a word, you can also search for beginning of a line  For example, “^I’m” will return a match for any line that begins with “I’m”. 


Hello world!


How are you today?


I’m fine, thanks!


If you had wanted to return a match for the whole line, you can use “^I’m.*”  (Remember that . is any character except line breaks)


Similarly, you can return matches of end of line.  For example “!$” (Match line endings of !) will return the following matches:


Hello world!


How are you today?


I’m fine, thanks!


Again, to return a match for a whole line you can use .*!”


Hello world!


How are you today?


I’m fine, thanks!



Stay tuned for more regular expression tips to come! 🙂


-Fiona


 



Comments (7)

  1. randy says:

    nice to see this!

    I never knew VS supported regex in the find and replace fields :-/

  2. Hi,

    I would like to look at Tutorial #1. Is that possible?

  3. theCoach says:

    Relatively new to regular expressions, and I have a concrete example where I am hoping to leverage them. I have two user controls that are a list of textboxes and labels that are generated using XSLT on a schema. The propblem that I have is that they do not match the naming convention. All of my items end up looking like this:

    this.TitleTextBox = new System.Windows.Forms.TextBox();

    // Instead of

    this.titleTextBox = new System.Windows.Forms.TextBox();

    /*

    So I want to change the uppercase Letter that follows "this." to its lowercase value. Does regular Expressions have that ability? Thanks

    */

  4. Fiona says:

    Hi David,

    Definately! tutorial #1 is still available on this blog, you can scroll down, or here’s the link

    http://blogs.msdn.com/vseditor/archive/2004/06/16/157276.aspx

    -Fiona

  5. Fiona says:

    Hi theCoach,

    Even though replace does allow to replace for tagged expressions, there is currently no way to take a tagged expression and change it on the fly (more on tagged expressions in a future tutorial).

    In your case, a search of "this.{:Ll}} will match "this.*", where * is any lower cased letter. Enclosing :Ll in {} makes it a tagged expression. In the replace field, you can enter the tagged expression into the replace field by using 1 (since it is tagged expression #1). However, since you can’t change any properties of a tagged expression on the fly (ie make it it’s uppercase counterpart), I’m afraid that it won’t be able to do the replace for you.

    Regards,

    Fiona

  6. I’v used a lot regular expression in Visual Studio, so, when I wanted to replace a pattern matching regular…