Visual Studio Support for Creating Your RegEx strings

Bells and Whistles for the new feature?

No, this trick works in all versions of Visual Studio. Well, at least all I can remember. But it is much more useful since VS switched to match the RegEx used in .NET in VS 2012.

You’re about to find out that I’m not really a RegEx genius. I can’t remember those details any better than you can – I’m just better at knowing where the easy to access hints are:

  • Create some sample data and open it in Visual Studio
  • Go to the old Find Dialog and RegEx in Find Options
  • Select Document scope from Look In combo
  • Push the button next to the Find What textbox
  • This popup reminds you of everything you always forget
  • Want more? Click RegEx help button for non-concise version
  • Push “Find in Files,” even though that feels illogical
  • All your matches will be found
  • Since the format sucks, fix it like this (both find and replace)

This gets you the line number for your match, which is often enough.

If you want to get the actual value, you need a little trick. My Replace result format, in my registry, is


I can get the actual values, all of them on each line, by doing a replace with an exact match of the search. You get that in VS Replace RegEx with $0. (If you know RegEx, VS captures the whole string as the first capture).)

Here’s a screen shot of the dialog (while that looks like an ugly RegEx, it’s just what you get selecting symbol match from the dropdown, I didn’t type anything).


And if I run this on a file that just contains

public _whatever123? test

The result is


This isn’t very intelligent RegEx for code, you’ll write something clever that makes sense with your data set.

Once you’ve got the RegEx string you want, copy it into your code and the call to RegEx, where it belongs.

It’s a little trickier to avoid messing up your data set if you are testing captures (Stop reading now if you have no idea what a “capture” means!)

Since it’s just sample data, I just do a series of replacement (massively messing the data file) and Ctl-Z for Undo. I delimit the captures, a dash doesn’t require escaping, and end the set of capture outputs with another delimiter (I use a set of asterisks) to differentiate the captures from the result string.


  • Input File (one line):        public _whatever123? Test
  • Search For (2 captures): (public) (\b(_\w+|[\w-[0-9_]]\w*)\b)
  • Replace With:                -$1- -$2- ***
  • Resulting File (Ctl-Z to undo): -public- -_whatever123- ***? test
  • And the resulting output is:


If you do a lot of RegEx, it’s absolutely worth your time to become familiar with third party RegEx tools.

But if you already know Visual Studio and you just want to stick an occasional RegEx string into your .NET code, or your debugging RegEx, or your mystified about why a RegEx statement looks like it does, use the Find and Replace dialogs in Visual Studio to make life much nicer!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>