溫馨提示×

c# match能否實現多模匹配

c#
小樊
115
2024-07-10 00:50:26
欄目: 編程語言

在C#中,Regex類提供了Match方法用于進行正則表達式匹配。Match方法只能返回第一個匹配項,無法實現多模匹配。要實現多模匹配,可以使用Regex.Matches方法,該方法返回所有與正則表達式模式匹配的項的集合。示例如下:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "Hello World! This is a test.";
        string pattern = @"\b\w{5}\b"; // 匹配5個字符的單詞

        MatchCollection matches = Regex.Matches(input, pattern);

        foreach (Match match in matches)
        {
            Console.WriteLine(match.Value);
        }
    }
}

上述代碼將輸出:

Hello
World
This

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女