关于C#中正则表达式

一 啥是正则表达式[微软]

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

  • 查找特定字符模式。
  • 验证文本以确保它匹配预定义模式(如电子邮件地址)。
  • 提取、编辑、替换或删除文本子字符串。
  • 将提取的字符串添加到集合中,以便生成报告。

ExplicitCapture:

      //
      // 摘要:
      //     指定唯一有效的捕获是显式命名或编号的 (?<name>…) 形式的组。这使未命名的圆括号可以充当非捕获组,并且不会使表达式的语法 (?:...) 显得笨拙。
 public static void Main2()
        {
            string input = "This is the first sentence. Is it the beginning " +
                           "of a literary masterpiece? I think not. Instead, " +
                           "it is a nonsensical paragraph.";
            string pattern = @"\b\(?((?>\w+),?\s?)+[\.!?]\)?";
            Console.WriteLine("With implicit captures:");
            foreach (Match match in Regex.Matches(input, pattern))
            {
                Console.WriteLine("The match: {0}", match.Value);
                int groupCtr = 0;
                foreach (Group group in match.Groups)
                {
                    Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value);
                    groupCtr++;
                    int captureCtr = 0;
                    foreach (Capture capture in group.Captures)
                    {
                        Console.WriteLine("      Capture {0}: {1}", captureCtr, capture.Value);
                        captureCtr++;
                    }
                }
            }
            Console.WriteLine();
            Console.WriteLine("With explicit captures only:");
            foreach (Match match in Regex.Matches(input, pattern, RegexOptions.ExplicitCapture))
            {
                Console.WriteLine("The match: {0}", match.Value);
                int groupCtr = 0;
                foreach (Group group in match.Groups)
                {
                    Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value);
                    groupCtr++;
                    int captureCtr = 0;
                    foreach (Capture capture in group.Captures)
                    {
                        Console.WriteLine("      Capture {0}: {1}", captureCtr, capture.Value);
                        captureCtr++;
                    }
                }
            }
        }
例子
With implicit captures:
The match: This is the first sentence.
   Group 0: This is the first sentence.
      Capture 0: This is the first sentence.
   Group 1: sentence
      Capture 0: This
      Capture 1: is
      Capture 2: the
      Capture 3: first
      Capture 4: sentence
The match: Is it the beginning of a literary masterpiece?
   Group 0: Is it the beginning of a literary masterpiece?
      Capture 0: Is it the beginning of a literary masterpiece?
   Group 1: masterpiece
      Capture 0: Is
      Capture 1: it
      Capture 2: the
      Capture 3: beginning
      Capture 4: of
      Capture 5: a
      Capture 6: literary
      Capture 7: masterpiece
The match: I think not.
   Group 0: I think not.
      Capture 0: I think not.
   Group 1: not
      Capture 0: I
      Capture 1: think
      Capture 2: not
The match: Instead, it is a nonsensical paragraph.
   Group 0: Instead, it is a nonsensical paragraph.
      Capture 0: Instead, it is a nonsensical paragraph.
   Group 1: paragraph
      Capture 0: Instead,
      Capture 1: it
      Capture 2: is
      Capture 3: a
      Capture 4: nonsensical
      Capture 5: paragraph

With explicit captures only:
The match: This is the first sentence.
   Group 0: This is the first sentence.
      Capture 0: This is the first sentence.
The match: Is it the beginning of a literary masterpiece?
   Group 0: Is it the beginning of a literary masterpiece?
      Capture 0: Is it the beginning of a literary masterpiece?
The match: I think not.
   Group 0: I think not.
      Capture 0: I think not.
The match: Instead, it is a nonsensical paragraph.
   Group 0: Instead, it is a nonsensical paragraph.
      Capture 0: Instead, it is a nonsensical paragraph.
结果

 

posted @ 2022-04-24 14:56  C#工控菜鸟  阅读(49)  评论(0编辑  收藏  举报