Created
August 25, 2016 02:47
-
-
Save noblethrasher/5edffb4cb2efa4ed9174fb9c82164bf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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