开发者

Parse string with regex

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 开发者_StackOverflow 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.


Try this:

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜