首页 新闻 赞助 找找看

c#如何使用正则表达式提取某字符串內的日期,并与当天做判断。[附不完整代码]

1
悬赏园豆:50 [已解决问题] 解决于 2015-09-23 17:11

c#如何使用正则表达式提取某字符串內的日期,并将多种日期变成统一格式的日期(统一换成(xxxx-xx-xx)这样的格式,并与当天日期做判断,如果匹配则返回日期,如果不匹配则返回空。本人新手自己尝试写了一下。不完善。希望各位老师帮忙看看!

 

 1 string txt="<div class="newsfunction"><span class="functiontuData">2015/09/21 10:53</span>" 
 2 
 3 string txt="<div class="newsfunction"><span class="functiontuData">2015-09-21 10:53</span>" 
 4 
 5 string txt="<div class="newsfunction"><span class="functiontuData">2015年09月21日 10:53</span>" 
 6 
 7 string txt="<div class="newsfunction"><span class="functiontuData">2015.09.21 10:53</span>" 
 8 
 9 //提取日期的正则表达式
10  Regex reg = new Regex(@"((?<!\d)((\d{2,4}(\.|年|\/|\-))((((0?[13578]|1[02])(\.|月|\/|\-))((3[01])|([12][0-9])|(0?[1-9])))|(0?2(\.|月|\/|\-)((2[0-8])|(1[0-9])|(0?[1-9])))|(((0?[469]|11)(\.|月|\/|\-))((30)|([12][0-9])|(0?[1-9]))))|((([0-9]{2})((0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))(\.|年|\/|\-))0?2(\.|月|\/|\-)29))日?(?!\d))");
G善源的主页 G善源 | 初学一级 | 园豆:91
提问于:2015-09-21 12:55
< >
分享
最佳答案
0
 1  public Form1()
 2         {
 3             InitializeComponent();
 4 
 5             string txt1 = "<div class=‘newsfunction’><span class='functiontuData'>2015/09/21 10:53</span>";
 6 
 7             string txt2 = "<div class=‘newsfunction’><span class='functiontuData'>2015-09-21 10:53</span>";
 8 
 9             string txt3 = "<div class='newsfunction'><span class='functiontuData'>2015年09月21日 10:53</span>";
10 
11              string txt4=  "<div class='newsfunction'><span class='functiontuData'>2015.09.21 10:53</span>";
12             //TEST
13              string s = run(txt1);
14              string s2 = run(txt2);
15              string s3 = run(txt3);
16              string s4 = run(txt4);
17         }
18         private string run(string input)
19         {
20               
21  
22            //提取日期的正则表达式
23            //  Regex reg = new Regex(@"((?<!\d)((\d{2,4}(\.|年|\/|\-))((((0?[13578]|1[02])(\.|月|\/|\-))((3[01])|([12][0-9])|(0?[1-9])))|(0?2(\.|月|\/|\-)((2[0-8])|(1[0-9])|(0?[1-9])))|(((0?[469]|11)(\.|月|\/|\-))((30)|([12][0-9])|(0?[1-9]))))|((([0-9]{2})((0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))(\.|年|\/|\-))0?2(\.|月|\/|\-)29))日?(?!\d))");
24 
25             Regex reg = new Regex("<span class='functiontuData'>(?<date>.*?)</span>", RegexOptions.CultureInvariant);
26            // MatchCollection mc in   reg.Matches
27 
28               foreach (Match m in reg.Matches(input))
29             {
30                 string sdate = m.Groups["date"].Value;
31                 sdate = sdate.Replace("", "-").Replace("", "-").Replace("", "").Replace(".", "-");
32                 DateTime dt=System.DateTime.Now.AddDays(1);
33                 //符合日期规范
34                 if (DateTime.TryParse(sdate, out dt))
35                 {
36                     //如果匹配则返回日期,如果不匹配则返回空。
37                     if (dt.ToShortDateString() == System.DateTime.Now.ToShortDateString())
38                     {
39                         string res = dt.ToString("yyyy-MM-dd");
40                         return res;
41                     }
42                     else
43                     {
44                         return "";
45                     }
46                 }
47               
48 
49             }
50             return "";
51 
52         }
收获园豆:50
JackWang-CUMT | 老鸟四级 |园豆:2866 | 2015-09-21 14:10

非常感谢你的代码,不过我有点疑问还希望老师解答下!

G善源 | 园豆:91 (初学一级) | 2015-09-21 14:31

@G善源: 

什么疑问,可以探讨一下

JackWang-CUMT | 园豆:2866 (老鸟四级) | 2015-09-21 14:33

@JackWang-CUMT: 

string txt = "2015/09/21 10:53";

            Regex reg = new Regex("(?<date>.*?)", RegexOptions.CultureInvariant);
            // MatchCollection mc in   reg.Matches

            foreach (Match m in reg.Matches(txt))
            {
                string sdate = m.Groups["date"].Value;
                sdate = sdate.Replace("", "-").Replace("", "-").Replace("", "").Replace(".", "-");
                DateTime dt = System.DateTime.Now.AddDays(1);
                //符合日期规范
                if (DateTime.TryParse(sdate, out dt))
                {
                    //如果匹配则返回日期,如果不匹配则返回空。
                    if (dt.ToShortDateString() == System.DateTime.Now.ToShortDateString())
                    {
                        string res = dt.ToString("yyyy-MM-dd");
                        Console.WriteLine(res);
                        Console.ReadKey();
                        //return res;
                    }
                    else
                    {
                        Console.ReadKey();
                        //return "";
                       
                        
                    }
                }
            
            }

我将代码换了一下,为什么测试运行就获取不到时间了。我是做一个采集器里面的一个小模块。就是判断是否是最新的资讯,这个采集器能够获取到时间,就是在一篇网页中提取出日期,但他是2015/09/21 10:53这个样的格式,还有有一些网页的日期是其他格式的。你能根据情况看我改了的这个代码吗?txt是一个变量,是采集器获取的日期。

G善源 | 园豆:91 (初学一级) | 2015-09-21 14:45

@G善源: 我用的采集器是火车采集器!不知道是否了解过。

G善源 | 园豆:91 (初学一级) | 2015-09-21 14:46

@G善源: 

如果能直接获取日期格式文本,则不需要用正则表达式,直接可以进行格式转换:

  string  sdate = txt;
             sdate = sdate.Replace("", "-").Replace("", "-").Replace("", "").Replace(".", "-");
             DateTime dt = System.DateTime.Now.AddDays(1);
             //符合日期规范
             if (DateTime.TryParse(sdate, out dt))
             {
                 //如果匹配则返回日期,如果不匹配则返回空。
                 if (dt.ToShortDateString() == System.DateTime.Now.ToShortDateString())
                 {
                     string res = dt.ToString("yyyy-MM-dd");
                     Console.WriteLine(res);
                     Console.ReadKey();
                     //return res;
                 }
                 else
                 {
                     Console.ReadKey();
                     //return "";


                 }
             }
JackWang-CUMT | 园豆:2866 (老鸟四级) | 2015-09-21 15:01

@JackWang-CUMT: 非常感谢,你完美的解决我的问题!

G善源 | 园豆:91 (初学一级) | 2015-09-23 17:11
其他回答(1)
0

1.取出时间字符串  (?<=class="functiontuData">).*?(?=</)

2.预先定义好多中时间字符串格式.并写好识别方法,输出datetime类型

3.识别时间字符串,输出datetime

4.格式化输出为你希望的格式

吴瑞祥 | 园豆:29449 (高人七级) | 2015-09-21 13:56

哥,能写个完整代码吗?

支持(0) 反对(0) G善源 | 园豆:91 (初学一级) | 2015-09-21 14:05

字符串是一个变量。我是想爬别人的网页,所以要对时间做处理!

支持(0) 反对(0) G善源 | 园豆:91 (初学一级) | 2015-09-21 14:06

@G善源: 也就是说你不确定人家的时间字符串是什么格式?

这样是做不了的.你能处理的只有已知格式的数据.

方法是在上面的第二步做扩展.

PS:我从不写代码

支持(0) 反对(0) 吴瑞祥 | 园豆:29449 (高人七级) | 2015-09-21 14:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册