从字符串C#查找日期时间 [英] Finding datetime from a string C#
问题描述
说我有一个字符串,它看起来像这样D1011608201313
Say I got a string that looks like this D1011608201313
第一部分是随机字母和媒体的部分是格式化像DD / MM / YYYY和日期梯记录的ID。然而,第一部分可以非常随意
The first part is random letters and the medium part is a date formatted like dd/mm/yyyy and the ladder an id for a record. However the first part can be pretty much random
像[随机字符串] [日期时间] [ID],我如何才能找到的日期时间的位置。 。随机字符串的长度,大约是4至8个字符
Like [Random String][DateTime][ID], how would I find the placement of the datetime. The length of the random string, is about 4 to 8 characters.
如果我能找到的日期时间,所以它应该是从那里直截了当:)
If I can find the datetime, it should be pretty straight forward from there :)
推荐答案
您可以使用这个表达式假设日期是在DDMMYYYY格式,日期是在今年一系列的1900年至2099年,但会有对于一些模糊的概率。我也更新这是基于关闭您的评论你的问题是,日期是从目前的月份。
You could use this RegEx supposing the date is in DDMMYYYY format, and the dates are in the year range of 1900-2099, but there will be some probability for ambiguity. I also updated this to be based off your comment in your question that the dates are from the current month.
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)的详细分解:
Detailed breakdown of the RegEx (from 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#查找日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!