Replace a line in a string


I have a string with many lines separated by carriage return. One of the line start with:
“ESD: …” The … is the date, of which varies from time to time.

How can I replace this line with: “ESD: the actual day in a string variable” ? Is there way to match the “ESD:…” up to the carriage return so that I can use the string.replace("old’Str, “newStr”)

Thanks a lot

Hi @Snowman

You could try this regex:


\r stands for carriage return char.

You can also look into using the Substring and IndexOf methods. If the date is always of constant length(01-01-2019, 23-01-2019 etc.), you should be able to remove it easily.

Dear srogo and q-z,

Thank you both, and will try out the recommended methods.


Hi @srogo ,

Suppose the line is:

ESD: 2019-08-22 abdbded

What would be the syntax, if I simply want to replace that line with “ABC” ?
NewText = OldText.Replace(“ESD:[^\r]*\r”, “ABC”)

Seems I misunderstand the syntax you advised.

Many thanks

Hi @srogo ,

After a number ot trial and error, I think I got your idea and make it right.

I use “ESD:[^\r]*” to get the whole line of “ESD:…”

The syntax is:
NewText = System.Text.RegularExpressions.Regex.Replace(OldText, “ESD:[^\r]*”, NewString)

Sorry should use “ESD:[^\n]*\n”

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.