次の ASP.NET C# regexp マッチを最も効率的な方法で実行しているかどうか疑問に思っていました。
入力文字列に一致させる必要がある HashSet に一連の正規表現があるので、次のようにします。
HashSet<string> hashMatchTo = new HashSet<string>();
hashMatchTo.Add(@"regexp 1");
hashMatchTo.Add(@"regexp 2");
hashMatchTo.Add(@"regexp 3");
hashMatchTo.Add(@"regexp 4");
hashMatchTo.Add(@"regexp 5");
//and so on
string strInputString = "Some string";
bool bMatched = false;
foreach (string strRegExp in hashMatchTo)
{
Regex rx = new Regex(strRegExp, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
if (rx.IsMatch(strInputString))
{
bMatched = true;
break;
}
}