Matching strings with a wildcard in C#

Wildcard characters in C# allow you to match patterns in strings where some characters are unknown. The most common wildcard is the asterisk (*), which represents zero or more characters. C# provides several ways to implement wildcard matching, including regular expressions and custom methods.

Using Regular Expressions for Wildcard Matching

Regular expressions are the most powerful way to implement wildcard pattern matching. The pattern \bt\S*s\b matches words that start with 't' and end with 's' −

Example

using System;
using System.Text.RegularExpressions;

namespace Demo {
   public class Program {
      private static void showMatch(string text, string expr) {
         MatchCollection mc = Regex.Matches(text, expr);
         foreach (Match m in mc) {
            Console.WriteLine(m);
         }
      }
      public static void Main(string[] args) {
         string str = "toss cross tacos texas";
         Console.WriteLine("Matching words that start with 't' and ends with 's':");
         showMatch(str, @"\bt\S*s\b");
      }
   }
}

The output of the above code is −

Matching words that start with 't' and ends with 's':
toss
tacos
texas

Simple Wildcard Matching with * and ?

For simpler wildcard patterns using * (zero or more characters) and ? (single character), you can convert them to regular expressions −

Example

using System;
using System.Text.RegularExpressions;

public class WildcardMatcher {
   public static bool IsMatch(string text, string pattern) {
      string regexPattern = "^" + Regex.Escape(pattern)
         .Replace(@"\*", ".*")
         .Replace(@"\?", ".") + "$";
      return Regex.IsMatch(text, regexPattern, RegexOptions.IgnoreCase);
   }

   public static void Main(string[] args) {
      string[] words = {"hello", "world", "help", "test"};
      string pattern = "he*";
      
      Console.WriteLine($"Words matching pattern '{pattern}':");
      foreach (string word in words) {
         if (IsMatch(word, pattern)) {
            Console.WriteLine(word);
         }
      }
      
      Console.WriteLine($"\nWords matching pattern 'h?l*':");
      foreach (string word in words) {
         if (IsMatch(word, "h?l*")) {
            Console.WriteLine(word);
         }
      }
   }
}

The output of the above code is −

Words matching pattern 'he*':
hello
help

Words matching pattern 'h?l*':
hello
help

File Path Wildcard Matching

Wildcard matching is commonly used for file filtering. Here's an example that matches file extensions −

Example

using System;
using System.Text.RegularExpressions;

public class FileWildcard {
   public static bool MatchesPattern(string filename, string pattern) {
      string regexPattern = "^" + Regex.Escape(pattern)
         .Replace(@"\*", ".*")
         .Replace(@"\?", ".") + "$";
      return Regex.IsMatch(filename, regexPattern, RegexOptions.IgnoreCase);
   }

   public static void Main(string[] args) {
      string[] files = {"document.txt", "image.jpg", "data.csv", "report.pdf", "test.txt"};
      
      Console.WriteLine("All .txt files:");
      foreach (string file in files) {
         if (MatchesPattern(file, "*.txt")) {
            Console.WriteLine(file);
         }
      }
      
      Console.WriteLine("\nFiles starting with 'd':");
      foreach (string file in files) {
         if (MatchesPattern(file, "d*")) {
            Console.WriteLine(file);
         }
      }
   }
}

The output of the above code is −

All .txt files:
document.txt
test.txt

Files starting with 'd':
document.txt
data.csv

Common Wildcard Patterns

Wildcard Meaning Regex Equivalent
* Zero or more characters .*
? Exactly one character .
[abc] Any character from the set [abc]
[a-z] Any character in the range [a-z]

Conclusion

Wildcard matching in C# is primarily achieved through regular expressions, which provide flexible pattern matching capabilities. For simple patterns using * and ?, you can convert them to regex patterns, while complex matching scenarios benefit from direct regex usage with word boundaries and character classes.

Updated on: 2026-03-17T07:04:35+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements