代码之家  ›  专栏  ›  技术社区  ›  Snake Eyes

删除regex不工作的\u字符[关闭]

  •  -3
  • Snake Eyes  · 技术社区  · 6 年前

    我收到IRC的以下文本:

    :art!art@irc.org private #timing :\u000314CODEERROR:\u0003 [\u000304X012\u0003] Server time is different than UTC
    

    我想把东西拿走 \u000314 等等。

    我试过

    var test = Regex.Replace(text, @"\\u0{3}3\d{0,2}", string.Empty);
    

    不工作意味着不删除字符。

    我也试过了:

    var test = Regex.Replace(text, @"\u0{3}3\d{0,2}", string.Empty);
    

    这里我收到一个十六进制数字不足的异常。

    看小提琴: https://dotnetfiddle.net/7H5G95

    1 回复  |  直到 6 年前
        1
  •  0
  •   dyukha    6 年前

    我不知道你有什么问题。以下代码(带第一个regex)有效:

    using System;
    using System.Text.RegularExpressions;
    
    
    public class Program
    {
        public static void Main()
        {
            var text = @":art!art@irc.org private #timing :\u000314CODEERROR:\u0003 [\u000304X012\u0003] Server time is different than UTC";
            var res = Regex.Replace(text, @"\\u0{3}3\d{0,2}", string.Empty);
            System.Console.WriteLine(res);
        }
    }
    

    输出:

    :art!art@irc.org private #timing :CODEERROR: [X012] Server time is different than UTC