MSDN中有例子如下: using System;
using System.Text.RegularExpressions; public class RegexTest
{
public static void RunTest()
{
int counter;
Match m;
CaptureCollection cc;
GroupCollection gc; // Look for groupings of "Abc".
Regex r = new Regex("(Abc)+");
// Define the string to search.
m = r.Match("XYZAbcAbcAbcXYZAbcAb");
gc = m.Groups; // Print the number of groups.
Console.WriteLine("Captured groups = " + gc.Count.ToString()); // Loop through each group.
for (int i=0; i < gc.Count; i++)
{
cc = gc[i].Captures;
counter = cc.Count;
// Print number of captures in this group.
Console.WriteLine("Captures count = " + counter.ToString());
// Loop through each capture in group.
for (int ii = 0; ii < counter; ii++)
{
// Print capture and position.
Console.WriteLine(cc[ii] + " Starts at character " +
cc[ii].Index);
}
}
} public static void Main() {
RunTest();
}
}输出为:
Captured groups = 2
Captures count = 1
AbcAbcAbc Starts at character 3
Captures count = 3
Abc Starts at character 3
Abc Starts at character 6
Abc Starts at character 9问题是:为什么只匹配了AbcAbcAbc和Abc,而没有AbcAbc?为什么Abc组中没有匹配到第15个字符处的?
using System.Text.RegularExpressions; public class RegexTest
{
public static void RunTest()
{
int counter;
Match m;
CaptureCollection cc;
GroupCollection gc; // Look for groupings of "Abc".
Regex r = new Regex("(Abc)+");
// Define the string to search.
m = r.Match("XYZAbcAbcAbcXYZAbcAb");
gc = m.Groups; // Print the number of groups.
Console.WriteLine("Captured groups = " + gc.Count.ToString()); // Loop through each group.
for (int i=0; i < gc.Count; i++)
{
cc = gc[i].Captures;
counter = cc.Count;
// Print number of captures in this group.
Console.WriteLine("Captures count = " + counter.ToString());
// Loop through each capture in group.
for (int ii = 0; ii < counter; ii++)
{
// Print capture and position.
Console.WriteLine(cc[ii] + " Starts at character " +
cc[ii].Index);
}
}
} public static void Main() {
RunTest();
}
}输出为:
Captured groups = 2
Captures count = 1
AbcAbcAbc Starts at character 3
Captures count = 3
Abc Starts at character 3
Abc Starts at character 6
Abc Starts at character 9问题是:为什么只匹配了AbcAbcAbc和Abc,而没有AbcAbc?为什么Abc组中没有匹配到第15个字符处的?
正则采用的是贪婪匹配,就是说会尽量多的去匹配
说得正确,不然你那AbcAbcAbc里也满足匹配单个(Abc)了
就是说只匹配最简单的一个(Abc)和最复杂的一个(AbcAbcAbc)吗?
那第二个问题呢?(为什么Abc组中没有匹配到第15个字符处的?)
AbcAbcAbc里已经匹配单个(Abc)了
输出中不是有:
Captures count = 3
Abc Starts at character 3
Abc Starts at character 6
Abc Starts at character 9
foreach(Match mm in mc)
Console.WriteLine(mm.Value);
在15个字符处的也匹配了,可以我这个程序的结果,就是把匹配的字符直接输出
楼主要分清以下几个的区别,如果不清楚,现在不方便写太多,晚上回去详细解释
Match
MatchCollection
CaptureCollection
GroupCollection 因为Match 只进行一次匹配,也就是遇到一个成功匹配项就停止匹配
Match 表示单个正则表达式匹配的结果,也就是返回第一个匹配成功项的结果,一旦找到一个匹配成功项,就将停止匹配
MatchCollection 检索整个字符串,找到匹配成功项的集合,也就是Match 的集合
GroupCollection 是正则表达式中捕捉组的集合,应用比较少
CaptureCollection Capture的集合,主要用于记录捕获组的过程值,应用也比较少这些内容可以参考我的博客2.2节
.NET正则基础——.NET正则类及方法应用回到例子,AbcAbcAbc是整个正则表达式匹配的结果
而Abc是每一次捕获组中间过程的捕获内容,也就是一个Capture捕获的内容
举个简单的例子
Regex r = new Regex("([a-z])+");
Match m = r.Match("abc123");
if (m.Success)
{
Console.WriteLine("整个Match匹配内容:{0} 匹配起始位置:{1}", m.Value, m.Index);
foreach (Capture c in m.Groups[1].Captures)
{
Console.WriteLine("每个Capture匹配内容:{0} 匹配起始位置:{1}", c.Value, c.Index);
}
}
Console.ReadLine();
/*-------输出--------
整个Match匹配内容:abc 匹配起始位置:0
每个Capture匹配内容:a 匹配起始位置:0
每个Capture匹配内容:b 匹配起始位置:1
每个Capture匹配内容:c 匹配起始位置:2
*/
这个例子应该是一目了然了,就不多加解释了例子中因为使用了Match ,所以只匹配到了AbcAbcAbc ,如果要位置15的Abc也一起匹配
Regex r = new Regex("(Abc)+");
MatchCollection mc = r.Matches("XYZAbcAbcAbcXYZAbcAb");
foreach (Match m in mc)
{
Console.WriteLine("匹配内容:{0} 匹配起始位置:{1}", m.Value, m.Index);
}
Console.ReadLine();
/*------输出-------
匹配内容:AbcAbcAbc 匹配起始位置:3
匹配内容:Abc 匹配起始位置:15
*/
ps:学习Regex
m = m.NextMatch();
gc = m.Groups;
for (int i = 0; i < gc.Count; i++)
{
cc = gc[i].Captures;
counter = cc.Count; // Print number of captures in this group.
Console.WriteLine("Captures count = " + counter.ToString()); // Loop through each capture in group.
for (int ii = 0; ii < counter; ii++)
{
// Print capture and position.
Console.WriteLine(cc[ii] + " Starts at character " +
cc[ii].Index);
}
}
http://msdn.microsoft.com/zh-cn/library/30wbz966(v=VS.80).aspx
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;namespace CSharpBit
{
class Program
{
static void Main(string[] args)
{
Regex r = new Regex("((a(b))c)+");
MatchCollection mc = r.Matches("abcabc123abc"); for (int j = 0; j < mc.Count; j++)
{
Match m = mc[j];
Console.WriteLine("第{0}个Match匹配内容:{1} 匹配起始位置:{2}",j , m.Value, m.Index);
Console.WriteLine("GroupCollection的个数 = {0}", m.Groups.Count);
for (int i = 0; i < m.Groups.Count; i++)
{
Console.WriteLine("GroupCollection={0}", m.Groups[i].Value);
foreach (Capture c in m.Groups[i].Captures)
{
Console.WriteLine("每个Capture匹配内容:{0} 匹配起始位置:{1}", c.Value, c.Index);
}
}
Console.WriteLine();
}
Console.ReadLine(); }
}
}输出:
GroupCollection的个数 = 4
GroupCollection=abcabc
每个Capture匹配内容:abcabc 匹配起始位置:0
GroupCollection=abc
每个Capture匹配内容:abc 匹配起始位置:0
每个Capture匹配内容:abc 匹配起始位置:3
GroupCollection=ab
每个Capture匹配内容:ab 匹配起始位置:0
每个Capture匹配内容:ab 匹配起始位置:3
GroupCollection=b
每个Capture匹配内容:b 匹配起始位置:1
每个Capture匹配内容:b 匹配起始位置:4第1个Match匹配内容:abc 匹配起始位置:9
GroupCollection的个数 = 4
GroupCollection=abc
每个Capture匹配内容:abc 匹配起始位置:9
GroupCollection=abc
每个Capture匹配内容:abc 匹配起始位置:9
GroupCollection=ab
每个Capture匹配内容:ab 匹配起始位置:9
GroupCollection=b
每个Capture匹配内容:b 匹配起始位置:10