In C#, I'm working with a small text file of variable length (it may have 4-6 lines).
Each line contains a repeating expression, followed by a 2-digit number and a colon and some concluding information.
For example:
some information 27: some other information some information 28: some other information some information 29: some other information
I need to remove all the characters up to and including the colon ':', leaving just the concluding information.
I have tried using a Regex expression as shown below. It correctly handles the first line in the file, but only the first line.
private static string ModifyTheInformation(string str)
{
string pattern = "^[^:\r\n]+:[ \t]*";
string replacement = "";
if (Regex.IsMatch(str, pattern, RegexOptions.Multiline))
{
str = Regex.Replace(str, pattern, replacement);
}
}
I need the Regex to loop through the file and affect all the lines. I have also tried using the Regex Match match method and match.NextMatch. For example:
Regex r = new Regex(pattern, RegexOptions.Multiline);
Match m = r.Match(str);
while (m.Success)
{
//try something
}
However, an instance variable does not offer the 'Replace' method.
Please suggest how I may use a Regex expression, which both Loops and can Replace the specified characters in each line of a text file.