zoukankan      html  css  js  c++  java
  • 分组匹配的一个示例


    这是今天回答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();
                    }

                }

            }

  • 相关阅读:
    基于 IAR 修改工程名称
    Baidu IoT Study
    msp430f5438a Information Flash Seg Write -- Chapter
    MFC 编辑框内容更新方法以及滚动条设置
    通过打开按钮打开文件和通过左键移动打开文件并计算crc
    移动文件并将文件路径显示到编辑框内
    Aritronix Virtual Device 运行
    将一个char类型的数转换成曼切斯特数
    数组中重复的数字
    平衡二叉树
  • 原文地址:https://www.cnblogs.com/sxlfybb/p/1247364.html
Copyright © 2011-2022 走看看