我有一个如下所示的代码
private bool Set(string stream, string inputdata)
{
bool retval = Regex.IsMatch(inputdata, stream, RegexOptions.IgnoreCase);
return retval;
}
static Dictionary<string, Regex> regexCache = new Dictionary<string, Regex>();
private Regex BuildRegex(string pattern)
{
Regex exp;
if (!regexCache.TryGetValue(pattern, out exp))
{
var newDict = new Dictionary<string, Regex>(regexCache);
exp = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
newDict.Add(pattern, exp);
regexCache = newDict;
}
return exp;
}
仍然是Regex.IsMatch我做了exp.IsMatch,但那是私有变量,所以我不知道如何继续那个
答案 0 :(得分:2)
private bool Set(string stream, string inputdata)
{
var regex = BuildRegex(stream);
bool retval = regex.IsMatch(inputdata);
return retval;
}
static Dictionary<string, Regex> regexCache = new Dictionary<string, Regex>();
private static Regex BuildRegex(string pattern)
{
Regex exp;
if (!regexCache.TryGetValue(pattern, out exp))
{
exp = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
regexCache.Add(pattern, exp);
}
return exp;
}