3

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

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

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

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

4

3 回答 3

2

假设日期为 DDMMYYYY 格式,并且日期在 1900-2099 年范围内,则可以使用此 RegEx,但可能会出现歧义。我还根据您在问题中的评论更新了这一点,即日期来自当月。

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}»
于 2013-08-22T13:38:56.320 回答
1

您可以在 RegEx 捕获组中捕获字符串的各个部分并单独引用它们。

var matches = Regex.Matches("D1011608201313",@".*([0-9]{2})([0-9]{2})([0-9]{4}).{2}$");
if (matches.Count!=0)
{
    var match = matches[0];
    var year = Convert.ToInt32(match.Groups[3].Value);
    var month = Convert.ToInt32(match.Groups[2].Value);
    var day = Convert.ToInt32(match.Groups[1].Value);
    var result = new DateTime (year,month,day); 
}
于 2013-08-22T13:55:20.440 回答
1

如果至少事先知道 ID,则可以保证通过 Regex 找到它

string result = Regex.Replace(source, @"^.*(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])(20[0-9][0-9])" + ID + @"$", "$1-$2-$3");
于 2013-08-22T13:47:22.103 回答