Converting wildcard to regular expression
I needed to build wildcard support into a C# app I am building. The user needs to be able to enter a string with wildcards that represent SQL table names. It’s pretty handy that the .NET Framework includes a regular expression parser in the System.Text.RegularExpressions namespace. However I couldn’t find a simple wildcard parser anywhere - you know, one that could handle the simple file system wildcards asterisk and question mark? I searched high and low but nothing turned up in my searches.
I’ve used regular expressions on occasion, so I needed to do a little bit of research to learn how to use them. I found a pretty nice article up on CodeProject that provides a nice introduction - Chapter 3 from the book Microsoft Visual C# .NET 2003 Developer’s Cookbook called Strings and Regular Expressions. Based on that I implemented an iterative solution.
The routine I wrote simply loops through the source string and processes each character. If it is a character that can be part of a regular expression, it escapes it with the backslash. If it is an asterisk, it replaces it with this string: “[\d\D]*”. This string says to include 0 or more instances of a digit or non-digit character. If it is a question mark it replaces it with a period: “.”, which says to allow 0 or 1 instance of a single character. As you can see, I am quite inexperienced with regular expressions, but this solution works great. I had to add a caret (“^”) at the beginning and a dollar sign (“$”) at the end so that it wouldn’t match any extra characters at the ends of the string.
I wasn’t completely happy with this solution, but it worked and it was time to move on. I decided to write about it and it’s a good thing I did. As I was looking for the above-mentioned article again, I found a solution on CodeProject that exactly matches my needs. It defines a class derived from the Regex class called Wildcard. It also implements a static method to do the actual conversion work.
Here’s what I learned from this article:
- I was having trouble simplifying the replacement for the asterisk. I tried [.]* but it just wouldn’t work and I didn’t know why, so I just reverted back to [\d\D]*. The Wildcard class uses simply .* (dot asterisk). Apparently you can only use square brackets if you have more than one character you want to modify with the asterisk.
- The Regex class provides a whole heck of a lot of functionality that I implemented myself. The Wildcard class - and the WildcardToRegex method in particular - is dirt simple compared to my original take on the problem.
- I’m not completely stupid
- while this solution is a whole lot simpler, I was doing everything in my solution that it does.
- I need to improve my search skills




Leave a Reply