I have a text file with some data like this:
:20:220112REG063a1
:23B:CRED
.
.
:20:220112REG064
:23B:CRED
.
.
:20:220112REG077
:23B:CRED
I want to find each instance of the line starting with ":20:" and replace the entire line with a new string.
The code I have is as follows:
rx = ":20:(.*)\r\n";
for (var m = Regex.Match(fileToString, rx); m.Success; m = m.NextMatch())
{
tag20 = createTransactionReferenceNumber();
fileToString = $"{fileToString.Substring(0, m.Index)}" + tag20 + $"{fileToString.Substring(m.Index + m.Length)}";
}
The above successfully replaces the first instance of the line but subsequent lines are "butchered". I am not sure how to fix this.
regex.Replace()methods which takes aMatchEvaluatorargument? learn.microsoft.com/en-us/dotnet/api/…fileToString = Regex.Replace(fileToString, rx, m => createTransactionReferenceNumber());or do you need to return$":20:{createTransactionReferenceNumber()}\r\n"?