Advertisement
Genesis2001

Untitled

Jan 6th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. // -----------------------------------------------------------------------------
  2. //  <copyright file="StringExtensions.cs" company="Zack Loveless">
  3. //      Copyright (c) Zack Loveless.  All rights reserved.
  4. //  </copyright>
  5. // -----------------------------------------------------------------------------
  6.  
  7. namespace Lantea.Common.Linq
  8. {
  9.     using System;
  10.     using System.Collections.Generic;
  11.     using System.Text;
  12.     using System.Text.RegularExpressions;
  13.  
  14.     public static partial class Extensions
  15.     {
  16.         // ReSharper disable InconsistentNaming
  17.         private static readonly Dictionary<string, Regex> regexes = new Dictionary<string, Regex>();
  18.         // ReSharper restore InconsistentNaming
  19.  
  20.         public static Match Match(this string source, string expression)
  21.         {
  22.             Regex r;
  23.  
  24.             if (regexes.TryGetValue(expression, out r))
  25.             {
  26.                 return r.Match(expression);
  27.             }
  28.  
  29.             r = new Regex(expression, RegexOptions.Compiled);
  30.             regexes.Add(expression, r);
  31.  
  32.             return r.Match(expression);
  33.         }
  34.  
  35.         public static bool Matches(this string source, string expression)
  36.         {
  37.             Regex r;
  38.  
  39.             if (regexes.TryGetValue(expression, out r))
  40.             {
  41.                 return r.IsMatch(source);
  42.             }
  43.  
  44.             r = new Regex(expression, RegexOptions.Compiled);
  45.             regexes.Add(expression, r);
  46.  
  47.             return r.IsMatch(source);
  48.         }
  49.        
  50.         /// <summary>
  51.         /// Attempts a regular expression match on the source string using the expression provided.
  52.         /// </summary>
  53.         /// <param name="source"></param>
  54.         /// <param name="expression"></param>
  55.         /// <param name="match">The matched expression object containing the groups that were matched.</param>
  56.         /// <returns>Returns True or False depending whether the match was successful.</returns>
  57.         public static bool TryMatch(this string source, string expression, out Match match)
  58.         {
  59.             Regex r;
  60.             if (regexes.TryGetValue(expression, out r))
  61.             {
  62.                 match = r.Match(source);
  63.                 return match.Success;
  64.             }
  65.  
  66.             r = new Regex(expression, RegexOptions.Compiled);
  67.             regexes.Add(expression, r);
  68.  
  69.             match = r.Match(source);
  70.             return match.Success;
  71.         }
  72.  
  73.         public static bool TryMatches(this string source, string expression, out MatchCollection match)
  74.         {
  75.             Regex r;
  76.             if (regexes.TryGetValue(expression, out r))
  77.             {
  78.                 match = r.Matches(source);
  79.                 return match.Count > 0;
  80.             }
  81.  
  82.             r = new Regex(expression, RegexOptions.Compiled);
  83.             regexes.Add(expression, r);
  84.  
  85.             match = r.Matches(source);
  86.             return match.Count > 0;
  87.         }
  88.  
  89.         public static StringBuilder AppendFormatLine(this StringBuilder source, string format, params object[] args)
  90.         {
  91.             source.AppendFormat(format, args);
  92.  
  93.             return source.Append(Environment.NewLine);
  94.         }
  95.  
  96.         public static bool EqualsIgnoreCase(this string source, string value)
  97.         {
  98.             return source.Equals(value, StringComparison.InvariantCultureIgnoreCase);
  99.         }
  100.  
  101.         public static bool StartsWithIgnoreCase(this string source, string value)
  102.         {
  103.             return source.StartsWith(value, StringComparison.InvariantCultureIgnoreCase);
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement