c# - 从字符串 C# 中查找日期时间

标签 c# regex datetime

假设我有一个看起来像这样的字符串 D1011608201313

第一部分是随机字母,中间部分是格式为 dd/mm/yyyy 的日期,阶梯是记录的 id。然而,第一部分可能非常随机

像 [Random String][DateTime][ID],我如何找到日期时间的位置。随机字符串的长度,大约4到8个字符。

如果我能找到日期时间,从那里应该很简单:)

最佳答案

假设日期为 DDMMYYYY 格式并且日期在 1900-2099 年范围内,您可以使用此正则表达式,但可能会出现歧义。我还根据您在问题中的评论更新了此日期,即日期是从当月开始的。

public static void Main()
{ 
  // Leaves room for ambiguity if the random prefix or index suffix look 
  // like dates as well.
  var pattern = "((0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])((19|20)[0-9]{2}))";

  // Or, I see in your comment that the dates are from the current month. 
  // If so then this decreases the probability of a false match. You could
  // use the following pattern instead:
  var year =  DateTime.Today.Year;
  var month  = string.Format("{0:00}", DateTime.Today.Month);
  pattern = "((0[1-9]|[12][0-9]|3[01])(" + month + ")(" + year + "))";        

  var str = "D1011608201313";
  var matches = Regex.Matches(str, pattern);
  if (matches.Count == 0) return;

  var groups = matches[0].Groups;     
  int d, m, y;
  int.TryParse(groups[2].Value, out d);
  int.TryParse(groups[3].Value, out m);
  int.TryParse(groups[4].Value, out y);
  var date = new DateTime(y, m, d);
  Console.WriteLine(date);
}

RegEx 的详分割类(来自 RegexBuddy):

((0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])((19|20)[0-9]{2}))

Match the regular expression below and capture its match into backreference number 1 «((0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])((19|20)[0-9]{2}))»
   Match the regular expression below and capture its match into backreference number 2 «(0[1-9]|[12][0-9]|3[01])»
      Match either the regular expression below (attempting the next alternative only if this one fails) «0[1-9]»
         Match the character “0” literally «0»
         Match a single character in the range between “1” and “9” «[1-9]»
      Or match regular expression number 2 below (attempting the next alternative only if this one fails) «[12][0-9]»
         Match a single character present in the list “12” «[12]»
         Match a single character in the range between “0” and “9” «[0-9]»
      Or match regular expression number 3 below (the entire group fails if this one fails to match) «3[01]»
         Match the character “3” literally «3»
         Match a single character present in the list “01” «[01]»
   Match the regular expression below and capture its match into backreference number 3 «(0[1-9]|1[012])»
      Match either the regular expression below (attempting the next alternative only if this one fails) «0[1-9]»
         Match the character “0” literally «0»
         Match a single character in the range between “1” and “9” «[1-9]»
      Or match regular expression number 2 below (the entire group fails if this one fails to match) «1[012]»
         Match the character “1” literally «1»
         Match a single character present in the list “012” «[012]»
   Match the regular expression below and capture its match into backreference number 4 «((19|20)[0-9]{2})»
      Match the regular expression below and capture its match into backreference number 5 «(19|20)»
         Match either the regular expression below (attempting the next alternative only if this one fails) «19»
            Match the characters “19” literally «19»
         Or match regular expression number 2 below (the entire group fails if this one fails to match) «20»
            Match the characters “20” literally «20»
      Match a single character in the range between “0” and “9” «[0-9]{2}»
         Exactly 2 times «{2}»

关于c# - 从字符串 C# 中查找日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18381997/

相关文章:

c# - 优化 Outlook 加载项

c# - 使用 ImmutableList(仅 Release模式)在 UWP 上使用 JSON.NET 反序列化问题

c# - 在标签之间查找文本并将其与标签一起替换

javascript - 捕获图案周围的尾随/前导空白(如果存在)?

c# - 来自字符串的新 DateTimeOffset

c# - 正在寻找其他方法来隐藏子类的抽象继承方法?

c# - 在 WinForms 中使用时间的最佳方法?

c++ - 正则表达式 ("(abc|aa.*|bb.*)") 与正则表达式 ("(aa.*|bb.*|cc.*)");

python - Pyspark 增加了几秒的时间

JavaScript 日期() : How to convert the local date to GMT