CSharp examples for System.Text.RegularExpressions:Regex
Wildcard To Regex Pattern
using System.Text.RegularExpressions; using System.Linq; using System.IO;/*from w w w .ja v a2 s . co m*/ using System.Collections.Generic; using System; public class Main{ public static string WildcardToRegexPattern(string wildcard, bool matchWholeWord = true) { if (String.IsNullOrEmpty(wildcard)) { wildcard = "*"; } // Replace ; with | string[] strings = wildcard.Split(';'); for (int i = 0; i < strings.Length; ++i) { // Remove extra spaces and escape any special characters and convert wildcards into regex tokens strings[i] = Regex.Escape(strings[i].Trim()).Replace("\\*", ".*").Replace("\\?", "."); } wildcard = string.Join("|", strings); // If the match whole word flag is set then add the required characters to the ends of the string if (matchWholeWord) wildcard = "^" + wildcard + "$"; return wildcard; } }