![]() |
General Programming »
Algorithms & Recipes »
Regular Expressions
Intermediate
Converting Wildcards to RegexesBy reinuxEver wondered how to do wildcards in C#? |
C#, Windows, .NET CF, .NET, Mobile, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Ever wondered how to do wildcards in .NET? It's not hard, all you have to do is use regular expressions. But it's not always easy to figure it out either. I found that I had to dig around for a while to figure out how to do it properly.
Even though regexes are a lot more powerful, wildcards are still good in situations where you can't expect the user to know or learn the cryptic syntax of regexes. The most obvious example is in the file search functionality of practically all OSs -- there aren't many that don't accept wildcards. I personally need wildcards to handle the HttpHandlers tag in web.config files.
Note: This method is good enough for most uses, but if you need every ounce of performance with wildcards, here is a good place to start.
There are three steps to converting a wildcard to a regex:
* and ?, so the rest of the text has to be converted to literals.
* becomes \* and ? becomes \?, so we have to convert \* and \? to their respective regex equivalents, .* and ..
^ and append $ to specify the beginning and end of the pattern. So, here's the golden function:
public static string WildcardToRegex(string pattern)
{
return "^" + Regex.Escape(pattern).
Replace("\\*", ".*").
Replace("\\?", ".") + "$";
}
Just to make it look cool, I wrapped it in a Wildcard class that inherits Regex.
/// <summary>
/// Represents a wildcard running on the
/// <see cref="System.Text.RegularExpressions"/> engine.
/// </summary>
public class Wildcard : Regex
{
/// <summary>
/// Initializes a wildcard with the given search pattern.
/// </summary>
/// <param name="pattern">The wildcard pattern to match.</param>
public Wildcard(string pattern)
: base(WildcardToRegex(pattern))
{
}
/// <summary>
/// Initializes a wildcard with the given search pattern and options.
/// </summary>
/// <param name="pattern">The wildcard pattern to match.</param>
/// <param name="options">A combination of one or more
/// <see cref="System.Text.RegexOptions"/>.</param>
public Wildcard(string pattern, RegexOptions options)
: base(WildcardToRegex(pattern), options)
{
}
/// <summary>
/// Converts a wildcard to a regex.
/// </summary>
/// <param name="pattern">The wildcard pattern to convert.</param>
/// <returns>A regex equivalent of the given wildcard.</returns>
public static string WildcardToRegex(string pattern)
{
return "^" + Regex.Escape(pattern).
Replace("\\*", ".*").
Replace("\\?", ".") + "$";
}
}
You can use it like any other Regex -- case-(in)sensitivity, string replacement, matching and all.
// Get a list of files in the My Documents folder
string[] files = System.IO.Directory.GetFiles(
System.Environment.GetFolderPath(
Environment.SpecialFolder.Personal));
// Create a new wildcard to search for all
// .txt files, regardless of case
Wildcard wildcard = new Wildcard("*.txt", RegexOptions.IgnoreCase);
// Print all matching files
foreach(string file in files)
if(wildcard.IsMatch(file))
Console.WriteLine(file);
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 15 Sep 2005 Editor: Smitha Vijayan |
Copyright 2005 by reinux Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |