简体   繁体   English

如何使用正则表达式从字符串中提取 dd-MMM-yyyy 中的日期

[英]How to extract date in dd-MMM-yyyy from string using regular expression

I have a string which contains a large amount of text and inside this text is a date in the format dd-MM-yyyy .我有一个包含大量文本的字符串,在此文本中是dd-MM-yyyy格式的日期。 An example of this text is as follows这段文字的一个例子如下

Composed by super, mtzshm to super, mtzshm on 31-Mar-2016 02:24 with Normal priority and Message Type General Message

I would like the date part of the text to be extracted using Regex and for it to be converted into a DateTime我希望使用 Regex 提取文本的日期部分并将其转换为 DateTime

For date time: \\d{2}-[Az]{3}-\\d{4}\\s\\d{2}:\\d{2}对于日期时间: \\d{2}-[Az]{3}-\\d{4}\\s\\d{2}:\\d{2}

For date: \\d{2}-[Az]{3}-\\d{4}对于日期: \\d{2}-[Az]{3}-\\d{4}

\d{2}       Match a digit of length 2
-           Matches character '-'
[A-z]{3}    Matches a character in the Range 'A' to 'z' of length 3
[A-z]*      Matches a character in the Range 'A' to 'z' of length x
\s          Matches any white space character
:           Matches character ':'

I'd like to know what the string will contains?我想知道字符串将包含什么? Will it contain only DD-MM-YYYY or will it have other things?它将只包含 DD-MM-YYYY 还是包含其他内容?

If you pass only DD-MM-YYYY Already and want to turn it into a DateTime you can do a simple for loop without the use of regular expression如果你只传递 DD-MM-YYYY 并且想要把它变成一个 DateTime 你可以做一个简单的 for 循环而不使用正则表达式

string date = "DD-MM-YYYY";

string year = null;
string month = null;
string day = null;

for ( int i = 0; i < 10; ++i)
{
   if ( i < 2)
     day += date[i];
   if ( i > 2 && i < 5 )
     month += date[i];
   if ( i > 5 )
     year += date[i];
}

DateTime date = new DateTime();
date = date.AddYears(Convert.ToInt32(year)-1);
date = date.AddMonths(Convert.ToInt32(month)-1);
date = date.AddDays(Convert.ToInt32(day-1);

This should work, just coded this directly on the site, tell me if that's not what you need, since there is not enough explanation.这应该可以工作,只是直接在网站上编码,如果这不是您需要的,请告诉我,因为没有足够的解释。

Edit : Nvm you want to get the 31-Mar-2016 , which means it is not a DD-MM-YYYY format but a DD-MMM-YYYY format which gives this regular expression :编辑:您想要获得 31-Mar-2016 的 Nvm ,这意味着它不是 DD-MM-YYYY 格式,而是 DD-MMM-YYYY 格式,它给出了这个正则表达式:

^(([0-9])|([0-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}$

Following regex will capture just Date part from given string.以下正则表达式将仅捕获给定字符串中的日期部分。

\b\d{1,2}\-[a-z]{3}\-\d{4}\b

Final code should look like最终代码应该看起来像

var text = "Composed by super, mtzshm to super, mtzshm on 31-Mar-2016 02:24 with ";
text    += "Normal priority and Message Type General Message";

var rx = new Regex(@"\b\d{1,2}\-[a-z]{3}\-\d{4}\b", RegexOptions.IgnoreCase | RegexOptions.Multiline);

var match = rx.Match(text);
if (match.Success)
{
    var dateString = match.Value;
    var date = DateTime.Parse(dateString);

    Console.WriteLine(date);
}

If everything goes well, you'll see a nice date printed in your local format.如果一切顺利,您将看到以本地格式打印的不错的日期。 In my case it'see在我的情况下,它看到

3/31/2016 12:00:00 AM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM