I need to replace a string where it follows a specific string and some varying data. I need to keep the beginning and the middle and just replace the end. When I tried the code below, it replaces only the last occurance. I have tried switching to a non greedy match but then it doesn't find it. The middle could contain new lines as well spaces, letters and numbers.
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.
How do I replace every occurance of "Old ending."