The Code Slinger

August 23, 2007

Regex in Visual Studio

Filed under: .NET,C#,Regular Expressions,Visual Studio 2008 — Pete @ 3:10 pm

If you’re a daily user of Visual Studio like I am, then you’ll appreciate this nice feature I recently actually figured out how to use: back-references in regular expressions built within search and/or search & replace operations.

Let’s say I have the following code from a VB to C# conversion, that incorrectly uses method parentheses ( ) when it should be using index brackets [ ] .

            if ((grid.Items(intCounter) is GridDataItem) && grid.Items(intCounter).Display)

            {

                CheckBox chkSelectForAction = (CheckBox)grid.Items(intCounter).FindControl(“chkVal”);

                if (chkSelectForAction.Checked & chkSelectForAction.Visible)

                {

                    ReturnedList.Add(grid.DataKeyValues(grid.Items(intCounter).ItemIndex)(“BindID”));

                }

            }

And now  let’s say this construct of “.Items(val)” is all over the place, and i want to change it to the correct “.Items[val]” construct.  The obvious problem is that for each match of “.Items(val)”  the inner value “val” may be completely different.

Here is where the power of regular expressions and backreferences helps us.

In the Visual Studio “Find & Replace” window, type the following into the “Find What:” box:

Items\({[^\)]+}\)

In the “Replace With: box, type:

Items\[\1\]

Now let’s explain.  In the first “find” regex, we tell it to find the literal “Items” then the character “(”  — using an escape character \( since we don’t mean to open a match expression.  Then we open the backreference block { }.  This tells VS that when a match is found, to store it in the first backreference (which is accessed via \1).  Then we tell it to find 1 or more of any character other than a closing parenthesis “)” — via [^\)] . This is so the regex doesn’t overrun the bounds of our indexer.  Then we close it up with closing the backreference block } and the ending parenthesis literal \).

Now, when each match of “Items(xxx)” is found, it will store xxx into the backreference of \1 (since there will be only one occurance of the “xxx” part in each match.

And in the same execution, it will fire the “Replace with” block, which accesses this backreference, replacing the entire match with Items[xxx].

And that’s it!  You can probably see how powerful this be when you consider global search and replace operations.  The only trick is to try out your new regex on a selection or single page prior to executing it within an entire project or solution (since you can’t “undo” that operation).

Til next time….keep on slangin!

Advertisements

2 Comments »

  1. Hi there everybody, here every one is sharing these know-how, thus it’s good to read
    this blog, and I used to pay a quick visit this website daily.

    Comment by porn — December 27, 2015 @ 2:23 am | Reply

  2. I think everything published made a lot of sense.
    But, what about this? suppose you were to create a killer title?
    I am not suggesting your information isn’t good, however what
    if you added a post title that grabbed a person’s attention? I mean Regex in Visual Studio | The Code
    Slinger is a little vanilla. You ought to look at Yahoo’s
    home page and see how they write article headlines to grab
    viewers to open the links. You might add a video or a related picture or two
    to grab readers excited about what you’ve written. Just my opinion, it would make your
    website a little bit more interesting.

    Comment by คลิปโป๊ — February 25, 2016 @ 11:01 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: