匹配正则表达式模式的字符串并替换为匹配的字符串的问题

时间:2020-03-31 13:17:16

标签: c# .net regex string replace

要求是包含表格式为text2的表数据的字符串变量dd/mm/yyyy hh:mm必须用="dd-MMM-yyyy HH:mm:ss"替换用双引号引起来的日期和时间

例如: 25-Feb-2020 15:27:58 需要替换为 ="25-Feb-2020 15:27:58"

DotNetFiddler

这是下面显示的完整代码段

using System;
using System.Text.RegularExpressions;


public class Program
{
    public static void Main()
    {
        string text = "<table>\n  <thead><tr><th style=\"\"><div class=\"th-inner \">Login Name</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner sortable\">Registered</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">Registered Date <br>Time</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner sortable\">User Response Count</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">Test Start Date Time</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">Test End Date Time</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">Time Remaining</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">User Status</div><div class=\"fht-cell\"></div></th></tr></thead><tbody><tr data-index=\"9\"><td style=\"\">njuser14</td><td style=\"\">Yes</td><td style=\"\">-</td><td style=\"\">0</td><td style=\"\">29-Feb-2020 15:27:58</td><td style=\"\">29-Feb-2020 15:28:03</td><td style=\"\">179</td><td style=\"\">Paused</td></tr><tr data-index=\"10\"><td style=\"\">njuser15</td><td style=\"\">Yes</td><td style=\"\">-</td><td style=\"\">0</td><td style=\"\">29-Feb-2020 15:27:32</td><td style=\"\">29-Feb-2020 15:27:42</td><td style=\"\">179</td><td style=\"\">Paused</td></tr></tbody></table>";
        string text2 = " dasd arew 2017-03-11 12:25:56 2017-03-11 12:25:56 das tfgwe 2017-03-11 12:25:56 ";
        string pattern = @"\d{4}\-\d{2}\-\d{2}\s\d{2}\:\d{2}\:\d{2}";
        Regex r = new Regex(pattern);
        var res = r.Replace(text, new MatchEvaluator(ConvertDateFormat));
        var res2 = r.Replace(text2, new MatchEvaluator(ConvertDateFormat));
        Console.WriteLine(res);
        Console.WriteLine("-------------------------------------------------------");
        Console.WriteLine(res2);
    }

    static string ConvertDateFormat(Match m)
    {
        var mydate = DateTime.Parse(m.Value);
        return mydate.ToString("=yyyy-MM-dd hh:mm:ss");
    }
}

// 29-Feb-2020 15:27:58 need to be replaced with ="29-Feb-2020 15:27:58"

结果:

<table>
  <thead><tr><th style=""><div class="th-inner ">Login Name</div><div class="fht-cell"></div></th><th style=""><div class="th-inner sortable">Registered</div><div class="fht-cell"></div></th><th style=""><div class="th-inner ">Registered Date <br>Time</div><div class="fht-cell"></div></th><th style=""><div class="th-inner sortable">User Response Count</div><div class="fht-cell"></div></th><th style=""><div class="th-inner ">Test Start Date Time</div><div class="fht-cell"></div></th><th style=""><div class="th-inner ">Test End Date Time</div><div class="fht-cell"></div></th><th style=""><div class="th-inner ">Time Remaining</div><div class="fht-cell"></div></th><th style=""><div class="th-inner ">User Status</div><div class="fht-cell"></div></th></tr></thead><tbody><tr data-index="9"><td style="">njuser14</td><td style="">Yes</td><td style="">-</td><td style="">0</td><td style="">29-Feb-2020 15:27:58</td><td style="">29-Feb-2020 15:28:03</td><td style="">179</td><td style="">Paused</td></tr><tr data-index="10"><td style="">njuser15</td><td style="">Yes</td><td style="">-</td><td style="">0</td><td style="">29-Feb-2020 15:27:32</td><td style="">29-Feb-2020 15:27:42</td><td style="">179</td><td style="">Paused</td></tr></tbody></table>
-------------------------------------------------------
 dasd arew =2017-03-11 12:25:56 =2017-03-11 12:25:56 das tfgwe =2017-03-11 12:25:56

但是这里是字符串变量

  1. text2 值已替换为 =dd-MMM-yyyy HH:mm:ss 。但不要 "=dd-MMM-yyyy HH:mm:ss"
  2. text 值保持不变。但不要 "=dd-MMM-yyyy HH:mm:ss"

1 个答案:

答案 0 :(得分:1)

根据评论,第一个问题似乎是期望

return mydate.ToString("=yyyy-MM-dd hh:mm:ss");

在将DataTime格式转换为字符串时,将包含引号。但是这些引号实际上是格式字符串本身的终止符,而不是格式字符串的一部分。

贾斯汀建议的解决方案

string.Format("=\"{0}\"", mydate.ToString("yyyy-MM-dd hh:mm:ss"))

尽管我的首选格式将使用字符串插值

$"\"{mydate.ToString("yyyy-MM-dd hh:mm:ss")}\""

第二个问题是text和text2具有不同的日期时间格式,并且提供的正则表达式仅匹配text2中的格式

text:  29-Feb-2020 15:27:58 
text2: 2017-03-11 12:25:56 
regex: @"\d{4}\-\d{2}\-\d{2}\s\d{2}\:\d{2}\:\d{2}"

正则表达式匹配字符串,并且不知道它们匹配的数据。因此,幼稚的文本正则表达式就像(未经测试的)

@"\d{2}\-[a-zA-Z]{3}\-\d{4}\s\d{2}\:\d{2}\:\d{2}"

这假设月份始终为3个字符,并且没有什么看起来像是一个非日期的日期。

您的示例显式地进行了2个不同的匹配,因此,如果这是您的工作方式,则可以为text和text2中的每一个创建一个新的正则表达式并进行多次替换。或者,您可以尝试结合正则表达式,例如(未测试):

@"\d{4}\-\d{2}\-\d{2}\s\d{2}\:\d{2}\:\d{2}|\d{2}\-[a-zA-Z]{3}\-\d{4}\s\d{2}\:\d{2}\:\d{2}"