Regex string parse

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

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

The first 18 characters are always the same, so they can easily t20>.

My problem is that the characters S12: not always equal to S12: it can be easily S26: - so I cannot use a simple String.Replace() on it. I need to replace these 3 characters with : 

How can I do this with regex? Thanks.

+1
source share
2 answers

Try the following:

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

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

0
source

Source: https://habr.com/ru/post/1238270/


All Articles