私はタイトルは正規表現を使用することになったと仮定するように変更された理由を尋ね上記のコメントをしました。
私は個人的に遅いので、Regexを使用しないようにしてください。 Regexは複雑な文字列パターンには最適ですが、文字列の置換が単純で、その中からいくつかのパフォーマンスが必要な場合は、Regexを使わずに方法を見つけようとします。
一緒にテストしました。 Regexと文字列メソッドで100万個の置換えを実行する
正規表現は26.5秒で完了しました。が完了すると、文字列メソッドは8秒になりました。
//Using Regex.
Regex r = new Regex(@"\b[Tt]he\b");
System.Diagnostics.Stopwatch stp = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < 1000000; i++)
{
string str = "The man is old. The is the Good. Them is the bad.";
str = r.Replace(str, "@@");
}
stp.Stop();
Console.WriteLine(stp.Elapsed);
//Using String Methods.
stp = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < 1000000; i++)
{
string str = "The man is old. The is the Good. Them is the bad.";
//Remove the The if the stirng starts with The.
if (str.StartsWith("The "))
{
str = str.Remove(0, "The ".Length);
str = str.Insert(0, "@@ ");
}
//Remove references The and the. We can probably
//assume a sentence will not end in the.
str = str.Replace(" The ", " @@ ");
str = str.Replace(" the ", " @@ ");
}
stp.Stop();
Console.WriteLine(stp.Elapsed);
なぜタイトルが正規表現を含めるように編集されましたか?テキスト置換を行う方法は、もっと多くあります。私は通常それが遅いのでRegexを避けるようにしています。タイトルは "仮定された"答えを含むように編集されていたはずです。 – Crispy
@Chris Persichetti:それは十分です。私はタイトルから "正規表現"を削除しました。 (私はタグに基づいて追加しましたが、 "正規表現"は明らかに元のタグの1つではありませんでした) –