在一個正則表達式中,若是要提取出多個不一樣的部分(子表達式項),須要用到分組功能。html
在 C# 正則表達式中,Regex 成員關係以下,其中 Group 是其分組處理類。正則表達式
Regex –> MatcheCollection (匹配項集合)編程
–> Match (單匹配項 內容)數組
–> GroupCollection (單匹配項中包含的 "(分組/子表達式項)" 集合)this
–> Group ( "(分組/子表達式項)" 內容)spa
–> CaputerCollection (分組項內容顯示基礎?).net
–> Caputercode
Group 對分組有兩種訪問方式:orm
一、數組下標訪問htm
在 ((\d+)([a-z]))\s+ 這個正則表達式裏總共包含了四個分組,按照默認的從左到右的匹配方式,
Groups[0] 表明了匹配項自己,也就是整個整個表達式 ((\d+)([a-z]))\s+
Groups[1] 表明了子表達式項 ((\d+)([a-z]))
Groups[2] 表明了子表達式項 (\d+)
Groups[3] 表明了子表達式項 ([a-z])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
string
text =
"1A 2B 3C 4D 5E 6F 7G 8H 9I 10J 11Q 12J 13K 14L 15M 16N ffee80 #800080"
;
Response.Write(text +
"<br/>"
);
string
strPatten =
@"((\d+)([a-z]))\s+"
;
Regex rex =
new
Regex(strPatten, RegexOptions.IgnoreCase);
MatchCollection matches = rex.Matches(text);
//提取匹配項
foreach
(Match match
in
matches)
{
GroupCollection groups = match.Groups;
Response.Write(
string
.Format(
"<br/>{0} 共有 {1} 個分組:{2}<br/>"
, match.Value, groups.Count, strPatten));
//提取匹配項內的分組信息
for
(
int
i = 0; i < groups.Count; i++)
{
Response.Write(
string
.Format(
"分組 {0} 爲 {1},位置爲 {2},長度爲 {3}<br/>"
, i
, groups[i].Value
, groups[i].Index
, groups[i].Length));
}
}
/*
* 輸出:
1A 2B 3C 4D 5E 6F 7G 8H 9I 10J 11Q 12J 13K 14L 15M 16N ffee80 #800080
1A 共有 4 個分組:((\d+)([a-z]))\s+
分組 0 爲 1A ,位置爲 0,長度爲 3
分組 1 爲 1A,位置爲 0,長度爲 2
分組 2 爲 1,位置爲 0,長度爲 1
分組 3 爲 A,位置爲 1,長度爲 1
....
*/
|
二、命名訪問
利用 (?<xxx>子表達式) 定義分組別名,這樣就能夠利用 Groups["xxx"] 進行訪問分組/子表達式內容。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
string
text =
"I've found this amazing URL at http://www.sohu.com, and then find ftp://ftp.sohu.comisbetter."
;
Response.Write(text +
"<br/>"
);
string
pattern =
@"\b(?<protocol>\S+)://(?<address>\S+)\b"
;
Response.Write(pattern.Replace(
"<"
,
"<"
).Replace(
">"
,
">"
) +
"<br/><br/>"
);
MatchCollection matches = Regex.Matches(text, pattern);
foreach
(Match match
in
matches)
{
GroupCollection groups = match.Groups;
Response.Write(
string
.Format(
"URL: {0}; Protocol: {1}; Address: {2} <br/>"
, match.Value
, groups[
"protocol"
].Value
, groups[
"address"
].Value));
}
/*
* 輸出
I've found this amazing URL at http://www.sohu.com, and then find ftp://ftp.sohu.comisbetter.
\b(?<protocol>\S+)://(?<address>\S+)\b
URL: http://www.sohu.com; Protocol: http; Address: www.sohu.com
URL: ftp://ftp.sohu.comisbetter; Protocol: ftp; Address: ftp.sohu.comisbetter
*/
|
內容參考自:
C#正則表達式編程(三):Match類和Group類用法 http://blog.csdn.net/zhoufoxcn/archive/2010/03/09/5358644.aspx
C#正則表達式類Match和Group類的理解 http://tech.ddvip.com/2008-10/122483707982616.html