首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用C#从字符串中提取日期的正则表达式

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

Stack Overflow用户
提问于 2018-01-03 22:33:35
回答 2查看 755关注 0票数 0

我是这样写代码的:

代码语言:javascript
运行
复制
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日到现在”“到现在”也可以是日期

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-04 17:18:09

代码语言:javascript
运行
复制
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}'");
}
票数 0
EN

Stack Overflow用户

发布于 2018-01-04 01:18:42

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

代码语言:javascript
运行
复制
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");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48079434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档