C#regex无法识别的转义序列

时间:2017-03-23 01:17:12

标签: c#

我想从蝙蝠行提取099%:4.16 / 099%检查它是否大于95并用高等级替换099%

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

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



            var my_string = "XXXXXXXXX" + "YYYYYYYYY" + "Bat:4.16/099%" + "BIT:7" + "AAAAAAAA" + "BBBBBBBBB" + "CCCCCCCCC";

            //I want to extract 099% from the line Bat:4.16/099% check if it is greater then 95 and replace 099% with High

            //The below code doesn't seem to replace at all. I don't know how to replace the second digit group alone with High
            Console.WriteLine(my_string);

            Regex regex_bat = new Regex("Bat:[^.*]\/[^\d+$]"); 
            my_string = regex_bat.Replace(my_string, "High"); 

            Console.WriteLine(my_string);
        }
    }
}

(26:52) Unrecognized escape sequence
(26:56) Unrecognized escape sequence

1 个答案:

答案 0 :(得分:0)

这样做:

 Regex regex_bat = new Regex("Bat:[^.*]\\//[^\\d+$]"); 

这将从099%替换为"高&#34 ;;

String getArr = my_string.Replace('%', '/');
String[] splt = getArr.Split('/');

if (int.Parse(splt[1].ToString()) > 95)
 { 
    splt[1] = "High";
 }
 string result = splt[0] + splt[1] + splt[2];

result =" XXXXXXXXXYYYYYYYYYBat:4.16HighBIT:7AAAAAAAABBBBBBBBBCCCCCCCCC"