請幫解讀一正規表示法 "((\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.])+
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"