次のコードがあります。
string pattern = @"(?:\S+\s){1,6}\S*" + search + @"\S*(?:\s\S+){1,6}";
String dbContents = row[2].ToString();
var matches = Regex.Matches(dbContents, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
for (int i = 0; i < matches.Count; i++)
{
if (i == 3)
break;
Contents += String.Format("... {0} ...", matches[i].Value);
}
私が達成しようとしているのは、検索語の前に 1 ~ 6 語、検索語の後に 1 ~ 6 語を取得することです。コードを実行すると、for ループ「matches.Count」でパフォーマンスが低下します。非常に大きな文字列では、実行に 1 分以上かかります。問題を解決するために何をすべきか、その理由について混乱しています。