Click here to Skip to main content
15,121,230 members
Articles / Mobile Apps
Posted 6 Sep 2005


67 bookmarked

Converting Wildcards to Regexes

Rate me:
Please Sign up or sign in to vote.
4.78/5 (57 votes)
15 Sep 20051 min read
Ever wondered how to do wildcards in C#?


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.

Using the Code

There are three steps to converting a wildcard to a regex:

  1. Escape the pattern to make it regex-safe. Wildcards use only * and ?, so the rest of the text has to be converted to literals.
  2. Once escaped, * becomes \* and ? becomes \?, so we have to convert \* and \? to their respective regex equivalents, .* and ..
  3. Prepend ^ 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(

// 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)


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Rei Miyasaka
Canada Canada
The cows are here to take me home now...

Comments and Discussions

QuestionLicense information Pin
gandhisachin2001@gmail.com8-Jul-19 8:57
Membergandhisachin2001@gmail.com8-Jul-19 8:57 
AnswerRe: License information Pin
gandhisachin2001@gmail.com31-Jul-19 21:04
Membergandhisachin2001@gmail.com31-Jul-19 21:04 
PraisePerfect solution Pin
Klixx26-Apr-17 22:09
MemberKlixx26-Apr-17 22:09 
QuestionLicense information Pin
Member 103749191-Nov-13 7:43
MemberMember 103749191-Nov-13 7:43 
QuestionThanks - just in time Pin
snoopy00116-May-13 5:31
Membersnoopy00116-May-13 5:31 
GeneralMy vote of 5 Pin
Christopher Drake2-Jan-13 10:05
MemberChristopher Drake2-Jan-13 10:05 
GeneralMy vote of 5 Pin
Donnerbalken24-Sep-12 0:33
MemberDonnerbalken24-Sep-12 0:33 
QuestionDoes not work with *.* Pin
guest2323113-Sep-12 4:23
Memberguest2323113-Sep-12 4:23 
GeneralMy vote of 5 Pin
Kuthuparakkal12-Sep-12 18:03
MemberKuthuparakkal12-Sep-12 18:03 
QuestionWord wild card matching behavior a little different from this PinPopular
weciii2-Mar-12 6:21
Memberweciii2-Mar-12 6:21 
GeneralMy vote of 5 Pin
vet0n23-Feb-12 22:46
Membervet0n23-Feb-12 22:46 
SuggestionAdding More Static Methods Pin
Sina Iravanian14-Dec-11 3:21
MemberSina Iravanian14-Dec-11 3:21 
QuestionRe: Adding More Static Methods Pin
DontSailBackwards6-May-13 15:47
MemberDontSailBackwards6-May-13 15:47 
AnswerRe: Adding More Static Methods Pin
drzaus1319-Sep-13 5:41
Memberdrzaus1319-Sep-13 5:41 
GeneralMy vote of 5 Pin
Nagy Vilmos8-Nov-10 3:24
professionalNagy Vilmos8-Nov-10 3:24 
Generalbug?? with '.' Pin
alchemy916-Nov-09 5:07
Memberalchemy916-Nov-09 5:07 
GeneralRe: bug?? with '.' Pin
Steve Kutnar2-Sep-10 7:28
MemberSteve Kutnar2-Sep-10 7:28 
QuestionHow to add other chars - [A-Z] [modified] Pin
boopathi_tpgit20-Oct-09 3:22
Memberboopathi_tpgit20-Oct-09 3:22 
GeneralDifference between httphandlers and httpmodules Pin
KothariGaurav20-Jun-08 1:20
MemberKothariGaurav20-Jun-08 1:20 
GeneralRe: Difference between httphandlers and httpmodules Pin
Rei Miyasaka20-Jun-08 7:24
MemberRei Miyasaka20-Jun-08 7:24 
NewsThere's a new version of the RegEx Tester Tool ! Pin
Pablo Osés2-Mar-08 0:18
MemberPablo Osés2-Mar-08 0:18 
GeneralSmall suggestion Pin
Davide Icardi16-Dec-07 23:12
MemberDavide Icardi16-Dec-07 23:12 
GeneralRe: Small suggestion Pin
philippe dykmans31-May-08 16:39
Memberphilippe dykmans31-May-08 16:39 
GeneralCorrection... Pin
philippe dykmans31-May-08 16:45
Memberphilippe dykmans31-May-08 16:45 
GeneralRe: Correction... Pin
Davide Icardi1-Jun-08 14:19
MemberDavide Icardi1-Jun-08 14:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.