日期:2014-05-17 浏览次数:21034 次
string period = this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 2].Cells["时期"].Value 
    
//这里以下的不会写呀            
string pattern = @""; 
                Regex regex = new Regex(pattern);
                MatchCollection mc = regex.Matches(period);
                if (mc.Count > 0)
                {
                    foreach (Match m in mc)
                    {
                        开始日期 = m.Groups[0].Value;
                        截止日期 = m.Groups[1].Value;
                    }
                }
string period = this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 2].Cells["时期"].Value 
    
//这里以下的不会写呀            
string pattern = @"((--)|(\d{4}-\d{2}-\d{2}))\s到\s((\d{4}-\d{2}-\d{2})|(--))"; 
                Regex regex = new Regex(pattern);
                MatchCollection mc = regex.Matches(period);
                if (mc.Count > 0)
                {
                    foreach (Match m in mc)
                    {
                        开始日期 = m.Groups[1].Value;
                        截止日期 = m.Groups[4].Value;
                    }
                }
------解决方案--------------------
            StreamReader reader = new StreamReader("c:\\1.txt",Encoding.Default);
            string source = reader.ReadToEnd();
            Regex reg = new Regex(@"(?is)(\d{4}-\d{2}-\d{2}) 到 (\d{4}-\d{2}-\d{2})");
            MatchCollection mc = reg.Matches(source);
            foreach (Match m in mc)
            {
                MessageBox.Show("开始:" + m.Groups[1].Value + "  结束:" + m.Groups[2].Value);
            }
------解决方案--------------------
string pattern = @"(\d{4}-\d{1,2}-\d{1,2}|--)\s*?到\s*?(\d{4}-\d{1,2}-\d{1,2}|--)";
                string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt
                var result = Regex.Matches(tempStr, pattern).Cast<Match>().Select(a => new { 
                    开始日期=a.Groups[1].Value,
                    结束日期=a.Groups[2].Value
                });
                /*
                 * +        [0]    { 开始日期 = "2012-05-31", 结束日期 = "2012-06-01" }    <Anonymous Type>
+        [1]    { 开始日期 = "2012-06-31", 结束日期 = "2012-08-01" }    <Anonymous Type>
+        [2]    { 开始日期 = "--", 结束日期 = "--" }    <Anonymous Type>
+        [3]    { 开始日期 = "2012-06-31", 结束日期 = "--" }    <Anonymous Type>
+        [4]    { 开始日期 = "--", 结束日期 = "2012-08-01" }    <Anonymous Type>
                 */