andrew4582

Wildcard

Feb 7th, 2013
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class MyClass
  6. {
  7.     public static void RunSnippet()
  8.     {
  9.         string text = "*or*";
  10.         string[] list = new String[]{"Hello World", "Hortel", "Wazzup"};
  11.         Wildcard wildcard = new Wildcard(text, RegexOptions.IgnoreCase);
  12.         foreach(var item in list)
  13.         {
  14.             if(wildcard.IsMatch(item))
  15.             {
  16.             WL(item);
  17.             }
  18.         }
  19.     }
  20.    
  21.     #region Helper methods
  22.    
  23.     public static void Main()
  24.     {
  25.         try
  26.         {
  27.             RunSnippet();
  28.         }
  29.         catch (Exception e)
  30.         {
  31.             string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
  32.             Console.WriteLine(error);
  33.         }
  34.         finally
  35.         {
  36.             Console.Write("Press any key to continue...");
  37.             Console.ReadKey();
  38.         }
  39.     }
  40.  
  41.     private static void WL(object text, params object[] args)
  42.     {
  43.         Console.WriteLine(text.ToString(), args);  
  44.     }
  45.    
  46.     private static void RL()
  47.     {
  48.         Console.ReadLine();
  49.     }
  50.    
  51.     private static void Break()
  52.     {
  53.         System.Diagnostics.Debugger.Break();
  54.     }
  55.  
  56.     #endregion
  57. }
  58.  
  59. /// <summary>
  60.         /// Represents a wildcard running on the
  61.         /// <see cref="System.Text.RegularExpressions"/> engine.
  62.         /// </summary>
  63.         public class Wildcard : Regex
  64.         {
  65.             /// <summary>
  66.             /// Initializes a wildcard with the given search pattern.
  67.             /// </summary>
  68.             /// <param name="pattern">The wildcard pattern to match.</param>
  69.             public Wildcard(string pattern)
  70.                 : base(WildcardToRegex(pattern))
  71.             {
  72.             }
  73.  
  74.             /// <summary>
  75.             /// Initializes a wildcard with the given search pattern and options.
  76.             /// </summary>
  77.             /// <param name="pattern">The wildcard pattern to match.</param>
  78.             /// <param name="options">A combination of one or more
  79.             /// <see cref="System.Text.RegexOptions"/>.</param>
  80.             public Wildcard(string pattern, RegexOptions options)
  81.                 : base(WildcardToRegex(pattern), options)
  82.             {
  83.             }
  84.  
  85.             /// <summary>
  86.             /// Converts a wildcard to a regex.
  87.             /// </summary>
  88.             /// <param name="pattern">The wildcard pattern to convert.</param>
  89.             /// <returns>A regex equivalent of the given wildcard.</returns>
  90.             public static string WildcardToRegex(string pattern)
  91.             {
  92.                 return "^" + Regex.Escape(pattern).
  93.                  Replace("\\*", ".*").
  94.                  Replace("\\?", ".") + "$";
  95.             }
  96.         }
Advertisement
Add Comment
Please, Sign In to add comment