特定の文字列といくつかの可変データに続く文字列を置き換える必要があります。最初と中間を残し、最後だけを置き換える必要があります。以下のコードを試してみると、最後の出現のみが置き換えられます。非貪欲な一致に切り替えようとしましたが、見つかりません。中央には、新しい行だけでなく、スペース、文字、数字を含めることができます。
String s = "Beginning of story. Keep this sentence. Old ending.\n";
s += s;
s += s;
s1 = Regex.Replace(s, @"Beginning of story. ([\s\S]*) Old ending.", "Beginning of story. " + @"$1" + " New ending.", RegexOptions.Multiline | RegexOptions.IgnoreCase);
The result is this:
Beginning of story. Keep this sentence. Old ending.
Beginning of story. Keep this sentence. Old ending.
Beginning of story. Keep this sentence. Old ending.
Beginning of story. Keep this sentence. New ending.
「古いエンディング」の出現をすべて置き換えるにはどうすればよいですか。