c#杂谈之三之正则表达式


一、正则表达式

正则表达式提供了功能强大、灵活而又高效的方法来处理文本。
正则表达式丰富的泛模式匹配表示法使你可以快速分析大量文本,以便:

  • 查找特定字符模式
  • 验证文本以确保它匹配预定义模式(如电子邮件地址)
  • 提取、编辑、替换或删除文本子字符串
  • 将提取的字符串添加到集合中,以便生成报告
    对于处理字符串或分析大文本块的许多应用程序而言,正则表达式是不可缺少的工具。

二、正则表达式的工作方式

使用正则表达式进行文本处理的核心是正则表达式引擎,(由 .NET 中的 System.Text.RegularExpressions.Regex 对象表示)。 使用正则表达式处理文本至少要求向该正则表达式引擎提供以下两方面的信息:

  • 要在文本中标识的正则表达式模式。
  • 要为正则表达式模式分析的文本。

Regex 类的方法使你可以执行以下操作:

  • 通过调用 Regex.IsMatch 方法确定输入文本中是否具有正则表达式模式。
  • 通过调用 Regex.Match 或 Regex.Matches 方法检索匹配正则表达式模式的一个或所有文本匹配项。 第一个方法返回提供有关匹配文本的信息的 System.Text.RegularExpressions.Match 对象。 第二个方法返回 MatchCollection 对象,该对象对于在分析的文本中找到的每个匹配项包含一个 System.Text.RegularExpressions.Match 对象。
  • 通过调用 Regex.Replace 方法替换匹配正则表达式模式的文本。

三、Regex类

看一下类的大概用法,后续用到在官方文档查具体的。


using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
  
        Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
          // RegxOptions.Compiled: 指定正则表达式编译为 MSIL 代码,而不是解释。
          // 已编译的正则表达式最大限度地提高运行时性能,代价是会影响初始化时间。 

          RegexOptions.Compiled | RegexOptions.IgnoreCase);


        string text = "The the quick brown fox  fox jumps over the lazy dog dog.";

        MatchCollection matches = rx.Matches(text);

        Console.WriteLine("{0} matches found in:\n   {1}",
                          matches.Count,
                          text);

        foreach (Match match in matches)
        {
            GroupCollection groups = match.Groups;
            Console.WriteLine("'{0}' repeated at positions {1} and {2}",
                              groups["word"].Value,
                              groups[0].Index,
                              groups[1].Index);
        }
    }
}

四、Match 类

表示单个正则表达式匹配的结果。
继承 Object Capture Group Match


using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
  
        string input = "int[] values = { 1, 2, 3 };\n" +
                     "for (int ctr = values.GetLowerBound(1); ctr <= values.GetUpperBound(1); ctr++)\n" +
                     "{\n" +
                     "   Console.Write(values[ctr]);\n" +
                     "   if (ctr < values.GetUpperBound(1))\n" +
                     "      Console.Write(\", \");\n" +
                     "}\n" +
                     "Console.WriteLine();\n";   
      
      string pattern = @"Console\.Write(Line)?";
      MatchCollection matches = Regex.Matches(input, pattern);
      foreach (Match match in matches)
         Console.WriteLine("'{0}' found in the source code at position {1}.",  
                           match.Value, match.Index);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永恒的宁静

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值