【问题标题】:Comma-separated time regex c#?逗号分隔的时间正则表达式 c#?
【发布时间】:2014-04-11 14:29:20
【问题描述】:

我正在尝试为以下模式编写正则表达式

pattern = "Time hh:mm separated by comma + # + Time hh:mm separated by comma";

我是正则表达式的新手,所以在学习了Quick start regex tutorial之后,我写了这个:

(([0-1][0-9]|[2][0-3]):([0-5][0-9]))+(,+(([0-1][0-9]|[2][0-3]):([0-5][0-9]))
?+,*)*+(#)+(([0-1][0-9]|[2][0-3]):([0-5][0-9]))+(,+(([0-1][0-9]|[2][0-3]):([0-5] 
[0-9]))?+,*)*

这个正则表达式有一些问题。它还匹配一些无效的字符串,例如:

 - 02:00,#03:00

 - 02:00#03:00,

有效字符串:

 - 02:00#03:00

 - 02:00,04:00,06:00#03:00

 - 02:00#03:00,06:00

而且它也不能正确创建组,我想按以下顺序创建组:

如果字符串是02:00,04:00,06:00#03:00,那么组应该是:

 - 02:00
 - 04:00
 - 06:00
 - #
 - 03:00

谁能帮我让这个正则表达式按预期工作?

【问题讨论】:

    标签: c# regex


    【解决方案1】:

    您可以在不使用 REGEX 的情况下做到这一点,例如:

    string str = "02:00,04:00,06:00#03:00";
    TimeSpan temp;
    bool ifValid = str.Split(new[] { ',', '#' })
                      .All(r => TimeSpan.TryParseExact(r, @"hh\:mm",CultureInfo.InvariantCulture, out temp));
    

    您可以将其提取到如下函数中:

    public bool CheckValid(string str)
    {
        TimeSpan temp;
        return str.Split(new[] { ',', '#' })
                          .All(r => TimeSpan.TryParseExact
                                                         (r, 
                                                          @"hh\:mm", 
                                                          CultureInfo.InvariantCulture, 
                                                          out temp));
    }
    

    然后检查工作如下:

    List<string> validStrings = new List<string>
    {
        "02:00#03:00",
        "02:00,04:00,06:00#03:00",
        "02:00#03:00,06:00"
    };
    Console.WriteLine("VALID Strings");
    Console.WriteLine("============================");
    foreach (var item in validStrings)
    {
        Console.WriteLine("Result: {0}, string: {1}", CheckValid(item), item);
    }
    
    Console.WriteLine("INVALID strings");
    Console.WriteLine("============================");
    List<string> invalidStrings = new List<string>
    {
        "02:00,#03:00",
         "02:00#03:00,",
    };
    
    foreach (var item in invalidStrings)
    {
        Console.WriteLine("Result: {0}, string: {1}", CheckValid(item), item);
    }
    

    输出将是:

    VALID Strings
    ============================
    Result: True, string: 02:00#03:00
    Result: True, string: 02:00,04:00,06:00#03:00
    Result: True, string: 02:00#03:00,06:00
    INVALID strings
    ============================
    Result: False, string: 02:00,#03:00
    Result: False, string: 02:00#03:00,
    

    【讨论】:

      【解决方案2】:
      \s(([0-1]\d|[2][0-3]):([0-5]\d))((,(([0-1]\d|[2][0-3]):([0-5]\d)))*)#(([0-1]\d|[2][0-3]):([0-5]\d))((,(([0-1]\d|[2][0-3]):([0-5]\d)))*)\s
      

      或者:

      String time = "(([0-1]\\d|[2][0-3]):([0-5]\\d))";
      String timeSequence = time + "((," + time + ")*)";
      String regex = "\\s" + timeSequence + "#" + timeSequence + "\\s";
      

      【讨论】:

      • 我试过你的正则表达式,但它与02:00#01:00, 匹配,这也是我在上面的问题中提到的无效字符串
      • 您对哪些类型的小时和分钟可以和不可以有更清晰的规范吗?
      • 在我的问题中我提到了一些有效和无效的字符串,请检查一下
      • 现在它既不匹配 02:00#01:00, 也不匹配任何有效字符串,如 02:00#01:00
      • 现在试试,让它更包含正确的表达方式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多