c# - 在 C# 中使用正则表达式解析日期

标签 c# regex

我正在开发 C# 应用程序,我得到一个包含日期或部分日期的字符串,我需要获取该字符串的日、月和年。例如:

string example='31-12-2010'
string day = Regex.Match(example, "REGULAR EXPRESSION FOR DAY").ToString();
string month = Regex.Match(example, "REGULAR EXPRESSION FOR MONTH").ToString()
string year = Regex.Match(example, "REGULAR EXPRESSION FOR YEAR").ToString()

day = "31"
month = "12"
year = "2010"

ex2: 
string example='12-2010'

string month = Regex.Match(example, "REGULAR EXPRESSION FOR MONTH").ToString()
string year = Regex.Match(example, "REGULAR EXPRESSION FOR YEAR").ToString()

month = "12"
year = "2010"

有什么想法吗?

最佳答案

不要为此使用正则表达式。 而是使用

DateTime temp = DateTime.Parse(example);

现在有很多有用的属性供您使用。 temp.Day 例如

关于c# - 在 C# 中使用正则表达式解析日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2730580/

相关文章:

C# asp.net- 如何从母版页访问内容页的方法?

javascript - Jquery 字符串与正则表达式匹配不起作用

python - 正则表达式给出元组而不是完全匹配

Java非贪婪(?)正则表达式来匹配字符串

java - 正则表达式替换重复字符

regex - ios NSRegularExpression 麻烦

c# - 如何确定我的应用程序的控制台窗口何时获得或失去焦点?

c# - 如何分辨 "if on development environment"?

c# - 64 位 .NET 应用程序中的 32 位 ActiveX 控件

c# IDsObjectPickerCredentials 失败