Skip to content

Instantly share code, notes, and snippets.

@noblethrasher
Created August 25, 2016 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noblethrasher/5edffb4cb2efa4ed9174fb9c82164bf3 to your computer and use it in GitHub Desktop.
Save noblethrasher/5edffb4cb2efa4ed9174fb9c82164bf3 to your computer and use it in GitHub Desktop.
static class RegexUtil
{
static readonly memo = new Dictionary<string, Regex>();
public static bool IsMatch(this string s, RegexOptions? options = null)
{
if(!memo.TryGetValue(s, out RegEx rgx))
memo.Add(s, rgx = new RegEx(s, options ?? RegExOptions.None));
return rgx.IsMatch(s);
}
}
//OR
struct regexp
{
readonly Regex regex;
public regexp(string s) => regex = new Regex(s);
public Match Match(string s) => regex.Match(s);
public bool IsMatch(string s) => regex.IsMatch(s);
public static implicit operator regexp(string s) => new regexp(s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment