分组匹配的一个示例


这是今天回答CSDN上一个网友的代码:

具体见:http://topic.csdn.net/u/20080720/21/17e58333-cd61-43b2-9a50-c80e2ae6453a.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace RegDemo
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            PrintOut();
        }


        
static string cc(Match m)
        
{
            
string str =  m.Groups["RV"].Value;
            
return m.Value.Replace(m.Groups["RV"].Value, "");
        }


        
static  void PrintOut()
        
{
            
using (StreamReader sr = new StreamReader("f:\\aa.txt"))
            
{
                
string str = sr.ReadToEnd();
                
if(!str.EndsWith("\\n"))
                
{
                    str 
= str + System.Environment.NewLine;
                }

                Console.WriteLine(
"content is :");
                Console.Write(str);
                
string pattern = ".*[0-2][0-9]:[0-6][0-9]:[0-6][0-9].*(?<RV>[0-2][0-9]:[0-6][0-9]:[0-6][0-9].*)\\n";
                Console.WriteLine();
                Regex reg 
= new Regex(pattern,RegexOptions.Multiline);
                
if(reg.IsMatch(str))
                
{
                    Console.WriteLine();
                    Console.WriteLine(
"now ,match success!");
                    Console.WriteLine();
                    
string strx = reg.Replace(str, new MatchEvaluator(cc));
                    Console.Write(strx);
                    Console.Read();
                }

            }

        }

    }

}


f:\aa.txt的内容:
1  03:30:18 快乐购物0716起播出            01:00:00
2  04:30:18 快乐购物0716起播出            01:00:00
3  05:30:18 快乐购物0716起播出            01:00:00
4  06:30:19 (胜者为王)宣传片            00:00:16
5  06:30:35 (锐车时代)宣传1            00:00:29 插播插播
6  06:31:04 (鹿鼎记片花3)              00:00:45
7  06:31:49 渝乐时光0716起播出            01:00:00 插播插播

运行结果:

运行结果:

content is :
1  03:30:18 快乐购物0716起播出            01:00:00
2  04:30:18 快乐购物0716起播出            01:00:00
3  05:30:18 快乐购物0716起播出            01:00:00
4  06:30:19 (胜者为王)宣传片            00:00:16
5  06:30:35 (锐车时代)宣传1            00:00:29 插播插播
6  06:31:04 (鹿鼎记片花3)              00:00:45
7  06:31:49 渝乐时光0716起播出            01:00:00 插播插播


now ,match success!

1  03:30:18 快乐购物0716起播出
2  04:30:18 快乐购物0716起播出
3  05:30:18 快乐购物0716起播出
4  06:30:19 (胜者为王)宣传片
5  06:30:35 (锐车时代)宣传1
6  06:31:04 (鹿鼎记片花3)
7  06:31:49 渝乐时光0716起播出

以上是使用分组替换的方法,或者也可以如下,输出结果是一样的


        static void PrintOut()
        
{
            
using (StreamReader sr = new StreamReader("f:\\aa.txt"))
            
{
                
string str = sr.ReadToEnd();
                
if (!str.EndsWith("\\n"))
                
{
                    str 
= str + System.Environment.NewLine;
                }

                Console.WriteLine(
"content is :");
                Console.Write(str);
                
string pattern = "[0-2][0-9]:[0-6][0-9]:[0-6][0-9].*?\\n";
                Console.WriteLine();
                Regex reg 
= new Regex(pattern, RegexOptions.Multiline|RegexOptions.RightToLeft);
                
if (reg.IsMatch(str))
                
{
                    Console.WriteLine();
                    Console.WriteLine(
"now ,match success!");
                    Console.WriteLine();
                    
string strx = reg.Replace(str,"\r\n");
                    Console.Write(strx);
                    Console.Read();
                }

            }

        }

posted on 2008-07-21 09:31  房客  阅读(332)  评论(1编辑  收藏  举报

导航