【问题标题】:Regex for Date and another Regex for Time日期的正则表达式和时间的另一个正则表达式
【发布时间】:2019-01-07 12:08:02
【问题描述】:

我正在编写一个返回日期和时间的代码(以 24 小时为单位)。我有一个在日期有效时匹配的正则表达式。我不完全知道它是否正常工作。另外,我当时需要一个正则表达式。它应该返回格式化的日期和时间。

这是一个应用程序,所以如果回复是在 c# 中,我将不胜感激。

感谢您抽出宝贵时间。

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

public class Example
{
 public static void Main()
   {
     string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", 
                        "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", 
                        "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", 
                        "M/d/yyyy h:mm", "M/d/yyyy h:mm", 
                        "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
                        "MM/d/yyyy HH:mm:ss.ffffff", 

                        "M-d-yyyy h:mm:ss tt", "M-d-yyyy h:mm tt", 
                        "MM-dd-yyyy hh:mm:ss", "M-d-yyyy h:mm:ss", 
                        "M-d-yyyy hh:mm tt", "M-d-yyyy hh tt", 
                        "M-d-yyyy h:mm", "M-d-yyyy h:mm", 
                        "MM-dd-yyyy hh:mm", "M-dd-yyyy hh:mm",
                        "MM-d-yyyy HH:mm:ss.ffffff" };

  string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM", 
                          "5/1/2009 6:32:00", "05/01/2009 06:32", 
                          "05/01/2009 06:32:00 PM", "05/01/2009 06:32:00",
                          "08/28/2015 16:17:39.125", "08/28/2015 
                           16:17:39.125000",

                          "5-1-2009 6:32 PM", "05-01-2009 6:32:05 PM", 
                          "5-1-2009 6:32:00", "05-01-2009 06:32", 
                          "05-01-2009 06:32:00 PM", "05-01-2009 06:32:00",
                          "08-28-2015 16:17:39.125", "08-28-2015 
                           16:17:39.125000" };
                           DateTime dateValue;

  string pattern = @"(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:1|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})";
  Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);  
  MatchCollection matches = rgx.Matches(input);
     if (matches.Count > 0)
{
  foreach (string dateString in dateStrings)
  {
     try {
        dateValue = DateTime.ParseExact(dateString, formats, new
                          CultureInfo("en-US"), DateTimeStyles.None);
        Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
         }
     catch (FormatException) { 
        Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
        }
      }
   }
}

【问题讨论】:

标签: javascript c# regex


【解决方案1】:

你可以用这个

var date = DateTime.Now.ToString("/*Here your format*/");

如果你想要 24 小时格式而不是忽略 tt。

【讨论】:

    【解决方案2】:

    日期的正则表达式,例如dd/mm/yyyy 格式的文件非常复杂,我还没有看到其中任何一个没有显示出异常。 请记住,您必须根据月份和年份部分的结果来验证月份部分的日期。 . .

    在一个非常复杂的正则表达式中可能(?)有一种方法可以做到这一点。但在代码函数中验证日期肯定更简单,更不容易出错。 请注意,在编码年份验证之前,您的 C# Date(...) 函数的年份组件的下限是已知的。我遇到了[这个奇怪的问题][https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Parameters] JavaScript Date(...) 的 1-99 范围内的年份组件,因此不得不相应地限制年份。

    时间正则表达式更容易。 hh:mm:ss 格式的 24 小时制的粗略枚举可能类似于:

    /[0][0-9]|[1][0-9]|[2][0-3]:[0-5][0-9]:[0-5] [0-9]/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-26
      • 2020-08-11
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      相关资源
      最近更新 更多