Regular expressions and Visual Studio

It can take a while to get to know how to use regular expressions well. After all most of us are used to


as our wild card language. It’s the old DOS style filtering that’s still used in windows to this day. Or perhaps you’re more used to SQL wild cards, eg:

‘%some text%’

Well, just because we didn’t have enough wild card standards now there’s regular expressions! They look more like:

^.*[a-zA-Z0-9 ]*

Regular expressions, though they do come with a bit of a learning curve, are actually a really great improvement on wild card languages. The biggest problem I’ve had with them so far is that the standard is not completely solid. The way they work in .Net, Java, Perl is very similar, but not exact! This can lead to some huge headaches when trying to write / port them.

One of the greatest features of Regular expressions is it’s ability to search and replace strings in files. If you’ve ever had a strange pattern that you needed to search for and didn’t know how, that’s where RegExp would come in.

Say you had a whole bunch of html that had href’s that looked like this:

<a href="/page?queryItem=1234">content</a>
<a href="/page?queryItem=125">content</a>
<a href="/page?queryItem=3456">content</a>
<a href="/page?queryItem=1289834">content</a>

and you were given a project that required those url’s change from
you would have allot of work on your hands! Well not any more. In Visual studio you can do a search and replace like this:
Find what:
Replace with:

What is happening here is the curly brackets {} are creating groups. These groups are numbered in the order they show up in the RegExp so the replace text is all just hardcoded text with two exceptions:
1) \1 this means use the content found in group 1 from the find
2) \. this is just the escape sequence for a period. A period in a RegExp means “any character” and must be escaped if you actually need a period.

What if you wanted to do it in another Regular expression parser you say? Well, it depends on the parser. I’ve found that the same replacement in another parser I regularly use requires the grouping not use curly braces {}, but round ones (). So the replacement would change to:

Find what:
Replace with:

That’s the only difference, of course it matters what your Regular expression contains. Some parsers require


if you want both upper and lower case, other parser allow one and call it case insensitive. In that case


are the same thing.

Best of luck with your Regular expressions.

Good reference:



If you’re new to programming you may not know what Recursion is.  Think file / folder structure in windows.  If you had to find every file in a folder and all it’s sub folders how would you do it?  Answer: recursion.

Here’s a handy little pattern snippit if you’re looking to do recursion on files / folders in .net:

        static void SomeRecursiveFunction(string sFolderPath)
            foreach (string sFile in System.IO.Directory.EnumerateFiles(sFolderPath))
                //Do work here
            foreach (string sDir in System.IO.Directory.EnumerateDirectories(sFolderPath))

Math.Pow is annoying – so I wrote my own

If you’ve not had to use it yet, Math.Pow() is annoying.

What it does: finds the ‘Power of’ x. So 2^2=4, 2^4=16, etc.

Why it’s annoying:

Well, there are a number of reasons. The first is it’s not the ^ symbol which is widely used by many programming languages (but not .Net).

The second is it requires decimal as it’s input type. Why MS? Why? I wanted to use the ulong datatype in my project but couldn’t get the power of it without converting it to decimal first. With large numbers this has the annoying feature of being expressed with .Nets exponential syntax like so:

I was working on a project that required me to calculate very large whole numbers and then write them to a file, this was an issue.

So, what I did was make it better by re-writing Math.Pow using ulong. I’m sure there are other ways to do this, but re-writing this function proved really simple to do. Feel free to use my method in your projects (though leaving a reference in the code to me would be nice!), also feel free to post alternative ways to solve this problem. I always enjoy your feedback!

//Pow() taken from Anthony's Tech Blog!
        static ulong Pow (ulong x, ulong y)
            if (y == 0) return 1;
            ulong count = x;
            for (ulong i = 0; i < y-1; i++)
                count = x * count;
            return count;

Linq – what it is

Linq (Language Integrated Query) is a great new way of fetching data from a database.  If you’ve ever written a data driven application then you know the pain of writing SQL to interact with your database.  It’s always seemed clunky to me, here you are creating objects with properties but the way those objects get saved to a database and read back out is in a language completely different from what you program in.

Linq is an attempt to change that.  Where a standard query looks like this:

Select col1, col2, col3 from myTable where col1='this'

a Linq query is written in your code and looks like this:

     var vProducts =
        from p in products
        where p.UnitsInStock > 0
        select p;

To view more Linq examples this is a great resource:
101 LINQ Samples: