tags:

views:

31

answers:

2
Field1: Unknown1
Field2: Unknown2
Field3: Unknown3

In my case I want to exclude row starting with "Field2:" which is effectively start marker "Field2:" end marker "\n" and replace with '' including delimiters.

Or what would be regular expression if I wanted to replace

Field2: Unknown\n  with Field2: SomethingElse\n
A: 

For this case, we can trivially use anchors to just match that line:

Regex.Replace(input, "^Field2:.*$", "Field2: SomethingElse");
Anon.
You need to set `RegexOptions.Multiline`, or it won't work because otherwise `^` and `$` match at the start/end of the string instead of start/end of the line.
Tim Pietzcker
+1  A: 

To delete the row entirely (assuming input is a string that has all the rows delimited with \n):

result = Regex.Replace(input, "^Fielt2:.*\n", "", RegexOptions.MultiLine);

To replace the row:

result = Regex.Replace(input, "^Field2:.*", "Field2: SomethingElse", RegexOptions.MultiLine);

Since .* grabs everything to the end of the line except the terminating line break, there's no need to use $ to match the end of the line.

Jan Goyvaerts