iT邦幫忙

1

請幫解讀一正規表示法 "((\w+)[\s.])+"

  • 分享至 

  • twitterImage

請幫解讀一正規表示法 "((\w+)[\s.])+"
這是一段在C#內的表示法
期望各位大大幫忙

比較不了解的是怎麼有這麼多種括號QQ

以下是原始代碼(中文標註的是我寫的,但是不知道正確不正確)

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string input = "Yes. This dog is very friendly.";
        string pattern = @"((\w+)[\s.])+";  //      \w    數字 字母 底線  \s  空白字元
                                            //        +   一個或多個        . 含空白鍵及且其後有字元(換行除外)
        foreach (Match match in Regex.Matches(input, pattern))
        {
            Console.WriteLine("Match: {0}", match.Value);
            for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
            {
                Group group = match.Groups[groupCtr];
                Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value);
                for (int captureCtr = 0; captureCtr < group.Captures.Count; captureCtr++)
                    Console.WriteLine("      Capture {0}: {1}", captureCtr,
                                      group.Captures[captureCtr].Value);
            }
        }
    }
}
// The example displays the following output:
//       Match: Yes.
//          Group 0: Yes.
//             Capture 0: Yes.
//          Group 1: Yes.
//             Capture 0: Yes.
//          Group 2: Yes
//             Capture 0: Yes
//       Match: This dog is very friendly.
//          Group 0: This dog is very friendly.
//             Capture 0: This dog is very friendly.
//          Group 1: friendly.
//             Capture 0: This
//             Capture 1: dog
//             Capture 2: is
//             Capture 3: very
//             Capture 4: friendly.
//          Group 2: friendly
//             Capture 0: This
//             Capture 1: dog
//             Capture 2: is
//             Capture 3: very
//             Capture 4: friendly
(\w+): 英文數字或底線一個以上
[\s.]: 後面接著空白或句號
(...)+: 最外面的括號表示裡面的組合可以重複 1~n 次

用途應該是匹配句子
該句子內可以包含空白或句號

例如:
This dog is very friendly
This dog is very friendly.
This dog is very.friendly.
lazywayne iT邦新手 5 級 ‧ 2020-03-12 11:02:37 檢舉
請問怎麼控制Capture 內容要有那些呢?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

5
listennn08
iT邦高手 5 級 ‧ 2020-03-11 15:58:36
最佳解答
((\w+)[\s.])+
Group1 ((\w+)[\s.]) => 群組比對 Group2 跟任何空白 + .
Group2 (\w+) => 群組比對 [a-zA-Z0-9_] 至少一次 到不限次數
\w =>  [a-zA-Z0-9_]
+ => 至少一次 到不限次數
[] => 字元的集合
\s => 任何空白 = [\r\n\t\f\v ]
+ => 至少一次 到不限次數

e.g.
"Yes. This dog is very friendly."
Match => "Yes."
Group1 => "Yes."
Group2 => "Yes"

我要發表回答

立即登入回答