首页 / C# / 从字符串C#中查找日期时间
从字符串C#中查找日期时间
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了从字符串C#中查找日期时间,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3387字,纯文字阅读大概需要5分钟。
内容图文
![从字符串C#中查找日期时间](/upload/InfoBanner/zyjiaocheng/678/306738bd5b4e4c6ca471fc8e47e0d73c.jpg)
说我有一个看起来像这样的字符串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}?
内容总结
以上是互联网集市为您收集整理的从字符串C#中查找日期时间全部内容,希望文章能够帮你解决从字符串C#中查找日期时间所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。