从字符串C#中查找日期时间
本文关键字:日期 时间 查找 字符串 | 更新日期: 2023-09-27 18:28:48
假设我得到一个字符串,看起来像D1011608201313
第一部分是随机字母,中间部分是格式类似dd/mm/yyyy的日期,梯形图是记录的id。然而,第一部分可以是相当随机的
就像[Drandom 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,您可以保证通过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");
您可以在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);
}