tags:

views:

68

answers:

2

Hello.

I have a string I need to parse. The problem is that some parts of the string is not always the same.

a3:S8:[gmpage]S17:Head GM NecrocideS12:test [15158]

The first 18 chars are always the same, so those can i String.Substring() out with ease.

My problem is that the characters S12: not always is S12:, it could easily be S26: - so i can not use a simple String.Replace() on it. I need to replace those 3 characters to : 

How can I do that with regex? Thank you.

+3  A: 

Try this:

string input = "a3:S8:[gmpage]S17:Head GM NecrocideS12:test [15158]";
string output = Regex.Replace(myString, "NecrocideS\d\d:", "Necrocide:");
Chris Pebble
A: 

How about:

Regex reg = new Regex(@"\A(?<before>a3:S8:\[gmpage\])(?<delete>.{3})(?<after>:Head GM NecrocideS12:test \[15158\])\Z");
string input = @"a3:S8:[gmpage]S26:Head GM NecrocideS12:test [15158]";
string output = reg.Replace(input, "${before}${after}");

This will replace S26 by ""

Nestor