专业的编程技术博客社区

网站首页 > 博客文章 正文

345.C# 中正则表达式的量词:掌握重复模式匹配

baijin 2024-09-03 09:55:32 博客文章 6 ℃ 0 评论

在 C# 中,正则表达式是一种强大的模式匹配工具,它通过 System.Text.RegularExpressions 命名空间中的 Regex 类提供支持。量词在正则表达式中扮演着重要的角色,它们定义了一个模式应当出现的次数。本文将介绍 C# 中正则表达式的量词,并提供示例以帮助理解。

量词的种类

在正则表达式中,量词可以分为几类:

  • *:匹配前面的元素零次或多次。
  • +:匹配前面的元素一次或多次。
  • ?:匹配前面的元素零次或一次。
  • {n}:匹配前面的元素恰好 n 次。
  • {n,}:匹配前面的元素至少 n 次。
  • {n,m}:匹配前面的元素至少 n 次,但不超过 m 次。

* 量词

* 量词表示前面的元素可以出现零次或多次。

示例

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string pattern = "bo*";
        string input = "A ghost boooed at me and I bolted.";
        foreach (Match match in Regex.Matches(input, pattern))
        {
            Console.WriteLine(#34;Found: {match.Value}");
        }
    }
}

输出:

Found: booo
Found: bo

+ 量词

+ 量词表示前面的元素至少出现一次。

示例

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string pattern = "bo+";
        string input = "A ghost boooed at me and I bolted.";
        foreach (Match match in Regex.Matches(input, pattern))
        {
            Console.WriteLine(#34;Found: {match.Value}");
        }
    }
}

输出:

Found: booo
Found: bo

? 量词

? 量词表示前面的元素可以出现零次或一次。

示例

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string pattern = "bo?";
        string input = "A ghost booed at me and I bolted.";
        foreach (Match match in Regex.Matches(input, pattern))
        {
            Console.WriteLine(#34;Found: {match.Value}");
        }
    }
}

输出:

Found: bo
Found: bo

{n} 量词

{n} 量词表示前面的元素必须恰好出现 n 次。

示例

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string pattern = "bo{2}";
        string input = "A ghost boooed at me and I bolted.";
        foreach (Match match in Regex.Matches(input, pattern))
        {
            Console.WriteLine(#34;Found: {match.Value}");
        }
    }
}

输出:

Found: boo

{n,} 量词

{n,} 量词表示前面的元素至少出现 n 次。

示例

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string pattern = "bo{2,}";
        string input = "A ghost boooed at me and I bolted.";
        foreach (Match match in Regex.Matches(input, pattern))
        {
            Console.WriteLine(#34;Found: {match.Value}");
        }
    }
}

输出:

Found: booo

{n,m} 量词

{n,m} 量词表示前面的元素至少出现 n 次,但不超过 m 次。

示例

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string pattern = "bo{1,2}";
        string input = "A ghost boooed at me and I bolted.";
        foreach (Match match in Regex.Matches(input, pattern))
        {
            Console.WriteLine(#34;Found: {match.Value}");
        }
    }
}

输出:

Found: boo
Found: bo

结论

量词在正则表达式中是非常重要的,因为它们定义了模式的重复次数。在 C# 中,通过 Regex 类和量词的使用,你可以创建灵活且强大的模式匹配规则来处理字符串数据。理解并掌握不同的量词对于编写高效的正则表达式至关重要。通过上述示例,你应该能够开始在自己的 C# 项目中使用量词来进行模式匹配了。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表