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

时间:2018-01-03 14:33:35

标签: c# regex

我正在写这样的代码:

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);
    }   
}

我想提取2014年11月13日到现在为止#34; "直到现在"也可以是约会

2 个答案:

答案 0 :(得分:0)

这是提取日期的基本正则表达式。

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");

答案 1 :(得分:0)

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}'");
}