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

说我有一个看起来像D1011608201313的字符串

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

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

如果我能找到日期时间,那应该是非常直接的:)

你可以使用这个RegEx,假设日期是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}» 

如果事先知道至少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"); 

您可以在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); }