A few words about Extension Methods and Regex builder library

LINQ is great. It’s what everyone has been talking about for some time now, and it’s the biggest thing that comes with .NET 3.5. But I guess that those saying that with time, people will appreciate other new things in C# 3.0/VB9.0 were right. I’m not even sure that LINQ will be THE feature I’m gonna use the most.

For some time now, in my spare time I’ve been working on a project that I’m going to publish up on codeplex. I know that I’ve chosen the wrong order here (first, created site for project, then started talking about it, and in the end, (hopefully) publish it), but what the heck… I already had to change plans about it, since right after I started working on it in spring I had to stop and move to other things, and it wasn’t until recently when I finally got some time for it.

In a few words, project is called Regular Expressions Builder Library and its name says it all. The idea is, to use skills you already have (some .NET language) to create regular expressions without having to learn all its rules, like what characters to escape, what is the name of that certain Unicode group and so on. Build regular expressions with strongly typed easy to comprehend manner with help of compile time checking and intellisence.

Funny thing is, that postulate of easiness seems to be the hardest to implement. The idea is to make the API as simple for someone with little idea about regular expressions as possible, so I decided to utilize Fluent Interfaces and name classes, properties and methods in such a manner, that it will be easy to read.

I started in .NET 2.0 and tried a few approaches, but the best I could come up with looked like this:

Pattern pattern = Pattern.Define().As(

    new Chars("Kot").





It’s not bad I guess, you can easily see what I want to accomplish with this regex pattern, but I was not satisfied with this new Chars(“Kot”). First of all, It’s a lot of typing to say “I want here those characters in this order”, second is that I really don’t want a new operator here. It has to do with simplicity and readability. Every pattern consists of at least one Group, every group consists of given sequences of characters (class Chars), any of (or except) characters from given group (class Any) and subGroups. To make it easier to read I wanted to save new operator for subgroups, and Chars and Anys start with some static method.

The unfunny thing is I couldn’t come up with anything concise and smarter than Chars.Sequence().

So then I decided to try another approach and move to .NET 3.5. It’s about time to start exploring its depths. I just started working with it so I don’t have much yet, but at least now, thanks to this little class:

public static class StringExtensions


    public static Chars Count(this string text, Times times)


        return new Chars(text).Count(times);



I’m able to rewrite first piece of code to look like this:

Pattern pattern = Pattern.Define().As(






Looks better, doesn’t it?

As almost everything however, Extensions Methods seems to be a double edged sword. You can pin methods to classes, but you can easily overdose, and have too many extension methods. Maybe it’s because I just started, but I think that, especially with such commonly used type as string, when you (as basically I’m going to) make string mimic other type, you can have it mimic yet more types, and instead if 4 types with 10 public members each, you end up with string having 40 options for you in intellisence. Good luck with that. Sad thing is, that since string is sealed, there’s not really much you can do about it.

Ok, this post is already too long so, I’ll post more about my adventures with C# 3.0 and Regex builder library next time. In the meanwhile? How do you like the API? How do you like the idea of such a library? If you have any comments please post them below or by mail, via form on this blog.


Nathan says:

I am interested in this library. I tried to download from codeplex but couldn’t find any releases or source code to download. What is the status of the project?