我需要替换一个字符串,它跟随一个特定的字符串和一些不同的数据。我需要保留开头和中间,只替换结尾。当我尝试下面的代码时,它只替换最后一次出现。我尝试切换到非贪婪匹配,但没有找到它。中间可以包含新行以及空格、字母和数字。
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.
如何替换每次出现的“旧结局”。