3

この D1011608201313 のような文字列を取得したとします。

最初の部分はランダムな文字で、中の部分は dd/mm/yyyy のような形式の日付で、はしごはレコードの ID です。ただし、最初の部分はかなりランダムになる可能性があります

[Random String][DateTime][ID] のように、datetime の配置を見つけるにはどうすればよいでしょうか。ランダム文字列の長さは、約 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);
}

正規表現の詳細な内訳 (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

少なくとも ID が事前にわかっている場合は、正規表現を介して確実に見つけることができます

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 に答える