Regex.Replace in C#string

时间:2017-03-26 23:11:40

标签: c#

我有以下字符串。我想从我的枚举TestType中替换%w[4] four

Ver:9.173/44.130
Db:5545 Snd:d545
Str:a1f6
%w[4]

public enum TestType : byte
{
    one = 1,
    two = 2,
    three = 3,
    four = 4,
    five = 5
}

作为第一步,我尝试了以下内容。

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;


    namespace Rextester
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                //Your code goes here

                var HexTestString = 
                             "0A5665723A392E3137332F34342E313330" +
                             "0A44623A3535343520536E643A643534350A5374723A613166360A0C25775b345d";


                var TestString = HexStringToString(HexTestString);
                Console.WriteLine(TestString);

                var WCString = UpdateWeaponCode(TestString);
                Console.WriteLine(WCString);

            }

            static string HexStringToString(string hexString)
            {
                //hexString.Replace(" ", string.Empty);
                if (hexString == null || (hexString.Length & 1) == 1)
                {
                    throw new ArgumentException();
                }
                var sb = new StringBuilder();
                for (var i = 0; i < hexString.Length; i += 2)
                {
                    var hexChar = hexString.Substring(i, 2);
                    sb.Append((char)Convert.ToByte(hexChar, 16));
                }
                return sb.ToString();
            }

            static string UpdateCode(string Test)
            {
                //StringBuilder builder = new StringBuilder(Test);
                //Regex regex_bat = new Regex("%w[^\\d+$]");

                string s = Regex.Replace(Test,"%w[\\d+]","Weapon");
                //Console.WriteLine(regex_bat);

                return s; //builder.ToString();

            } 
        }
    }

我得到的输出是

Ver:9.173/44.130
Db:5545 Snd:d545
Str:a1f6
%w[4]

首先不是用“武器”替换%w [4]。

0 个答案:

没有答案