1

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
  • By S12, do you mean S17? Commented Oct 29, 2009 at 20:57
  • No there is something in the line NecrocideS12:test :-) Commented Oct 29, 2009 at 20:58
  • Oops, missed the S12 at the end of Necrocide because of the coloring Commented Oct 29, 2009 at 20:59

2 Answers 2

3

Try this:

string input = "a3:S8:[gmpage]S17:Head GM NecrocideS12:test [15158]";
string output = Regex.Replace(myString, "NecrocideS\d\d:", "Necrocide:");
Sign up to request clarification or add additional context in comments.

Comments

0

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 ""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.