简体   繁体   English

使用C#从字符串中提取日期的正则表达式

[英]Regular Expression to extract date from a string using C#

I am writing the code like this : 我正在写这样的代码:

static void Main(string[] args)
{
    String str = "Web Application Developer in Acme Infosystem Mohali from 13 Nov 2014 to till present yii2 framework and Node JS. ";

    String rx = @"^(?:(?:31(\/|-|\.| )(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.| )(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.| )(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.| )(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$";
    //String rx = @"\d{ 1,2} \-[A - Z]{ 3} \-\d{ 1,2}";
    //String rx = @"\d{1,2}\-[A-Z]{3}\-\d{1,2}";
    //String rx = @"/\d+\-[A-Z]+\-\d+/";
    Regex ptrn = new Regex(rx);
    Match match = ptrn.Match(str);
    GroupCollection coll = match.Groups;
    foreach (var item in coll)
    {
        Console.WriteLine(item);
    }   
}

I want to extract "13 Nov 2014 to till present" "till present" can also be a date 我想提取“ 2014年11月13日至今”“直到现在”也可以是一个日期

Here's a basic regex for extracting the date. 这是提取日期的基本正则表达式。

string str = "Web Application Developer in Acme Infosystem Mohali from 13 Nov 2014 to till present yii2 framework and Node JS. ";

// (?<=from )  -- find a string after "from"
// \d{1,2} [A-Z][a-z]+ \d{4}   -- 1-2 digits, the month (capitalized), 4-digit year
// to  -- include the "to"
// (till present|\d{1,2} [A-Z][a-z]+ \d{4})"  -- "till present" OR another date
// -- The space characters in the regex are important, too.
string rx = @"(?<=from )\d{1,2} [A-Z][a-z]+ \d{4} to (till present|\d{1,2} [A-Z][a-z]+ \d{4})";

System.Text.RegularExpressions.Regex ptrn = new System.Text.RegularExpressions.Regex(rx);
System.Text.RegularExpressions.Match match = ptrn.Match(str);
if (match.Success)
    Console.WriteLine(match.Value);
else
    Console.WriteLine("No match found");
static void TestRegex()
{
    string s = "Web Application Developer in Acme Infosystem Mohali from 13 Nov 2014 to till present yii2 framework and Node JS.";
    string pattern = 
        @"(?x)
        (3[01]|2[0-9]|1[0-9]|0[1-9])                    # Matches a day
        \s+                                             # Matches spaces
        (Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)   # Matches month
        \s+                                             # Matches space
        [0-9]{4}                                        # Matches year";
    string firstDate = null, secondDate = null;
    var firstMatch = Regex.Match(s, pattern);
    if (firstMatch.Success)
    {
        firstDate = firstMatch.Value;
        // If first date is found, try to match second date
        var secondMatch = firstMatch.NextMatch();
        if (secondMatch.Success)
        {
            secondDate = secondMatch.Value;
        }
        else
        {
            secondDate = Regex.IsMatch(s, "to till present") ? "till present" : null;
        }
    }
    else
    {
        Console.WriteLine("First date not found.");
    }
    Console.WriteLine($"First date: '{firstDate}', secondDate: '{secondDate}'");
}

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

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