正则表达式-字符串后跟日期 [英] Regular expression - String followed by a date

查看:116
本文介绍了正则表达式-字符串后跟日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


任何人都可以给我一个字符串后跟日期的正则表达式.我可以在C#代码页中使用此正则表达式.

例如出生日期:1989年12月7日"

Hi
Can anybody give me the regular expression for a string followed by a date.I can use this regular expression in c# code page.

for example "Date of Birth : 07/12/1989"

推荐答案

string input = "Date of Birth : 07/12/1989";
Regex regex = new Regex(@"(?<info>.+)\s*:\s*(?<date>\d+/\d+/\d+)");
Match match = regex.Match(input);
if (match.Success)
{
    string info = match.Groups["info"].Value;
    string dateString = match.Groups["date"].Value;
    DateTime date = DateTime.Parse(dateString, System.Globalization.DateTimeFormatInfo.InvariantInfo);
}





在这里,我只是根据您的要求做了一些代码.

这仅用于测试.

Hi


Here i just did some code for your requirement.

this is just for testing only.

string Datestring = "Date of Birth : 07/12/1989";
   if (Datestring.IndexOf(':') > 0)
   {
       string datestr = Datestring.Split(':')[0];
       string datefrmt = Datestring.Split(':')[1];
        //you can test this based on REgEp also.
       if (datefrmt.IndexOf('/') > 0)
       {
           datefrmt = datefrmt.Trim();
           string date1 = datefrmt.Split('/')[0];
           string mnt = datefrmt.Split('/')[1];
           string yr = datefrmt.Split('/')[2];
           int datr, mntr, yre;
           if (int.TryParse(date1, out datr) && int.TryParse(mnt, out mntr) && int.TryParse(yr, out yre))
           {
               //entered string matched with one of date format
           }
       }
       else
       {
           if (datefrmt.IndexOf('-') > 0)
           {
               //Date of Birth : 07-Dec-1989
           }
           else
           {
               //Date of Birth : 07th Dec 1989
               string date1 = datefrmt.Split(' ')[1];
               string mnt = datefrmt.Split(' ')[2];
               string yr = datefrmt.Split(' ')[3];
               int datr, mntr, yre;
               int monthIndex;

               string[] MonthNames = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
               monthIndex = Array.IndexOf(MonthNames, mnt) + 1;

               if (int.TryParse(date1.Replace("th",""), out datr) && (monthIndex>0) && int.TryParse(yr, out yre))
               {
                   //entered string matched with one of date format
               }
           }
       }
   }



在上面的代码中,您需要更改一些内容以实现您的要求,这只是为了让您知道如何做.

而且我也确实用空白输入了您想要的内容.

我希望你能理解我的所作所为.

您可以使用正则表达式来减少该代码.

最好的



In the above code you need to change some to achieve your requirement this is just for giving you idea how to do.

and i did exactly string what you want with white spaces also.

I hope you understood what I did.

you can reduce that code using Regular Expressions.

All the Best


使用正则表达式来匹配标签和标签后面的日期文本.然后使用DateTime.TryParse(...).请参见 DateTime.TryParse(...) [
Use the regex to match the label and the date text after the label. Then employ DateTime.TryParse(...). See the DateTime.TryParse(...)[^] - there are good examples about the issues on parsing "any" date format (basically: not possible in general: you have to make a choice on what culture(s) youo want to try parsing, and what the time zone is (it this is important to your solution).

var match = Regex.Match(s, "^Date of Birth\s*:\s*(.*?)\s*


这篇关于正则表达式-字符串后跟日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆