C#regex未识别的转义序列
我想从Bat行中提取099%:4.16/099%,检查它是否大于95,并用High替换099%C#regex未识别的转义序列,c#,C#,我想从Bat行中提取099%:4.16/099%,检查它是否大于95,并用High替换099% using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] ar
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
这样做:
Regex regex_bat = new Regex("Bat:[^.*]\\//[^\\d+$]");
这将从099%替换为“高”
result=“xxxxxxxx yyyyyyyyyyyy bat:4.16高位:7aaaaaaaaaaaaaabbbbbbbccccc”@reds-错误现在消失了。但是下一行没有被重放。下一行是什么意思。你需要什么输出?@reds-我想从
Bat:4.16/099%
行中提取099%
,检查它是否大于95
,并用High
替换099%
。我不知道如何用High
单独替换第二个数字组(?即,仅当前面有Bat:blah/
@时,匹配1个或多个数字,然后%
。@如何检查它是否大于095?如果小于95,我想说低,请不要开始一个变色龙问题。您有“无法识别的转义序列”的答案“问题。如果您有其他问题需要帮助,请随时打开。提示:使用MatchEvaluator
代理
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];