读书人

搞不明白正则表达式相关类中的Capture

发布时间: 2012-05-28 17:59:54 作者: rapoo

搞不明白正则表达式相关类中的Captures属性

C# code
        public static void Main(string[] args)        {            string s = "a123 b456 c789";            Regex reg = new Regex(@"[a-z](\d+)");            Match match = reg.Match(s);            Console.WriteLine(match.Groups[1].Value);            Console.WriteLine(match.Groups[1].Captures[0].Value);            Console.WriteLine(match.Captures[0].Value);        }

通常我都是用match.Groups[1].Value或match.Groups["name"].Value的形式来取得我要的东东的。
请问什么情况下会用到Captures属性?或者说用它比较方便。

[解决办法]
http://developer.51cto.com/art/200908/145296.htm
[解决办法]
Captures

这个是用来获取,同一个组多次获取时的数据。

如:
"abc(?<a>\d\t)*"

这样,如果a组匹配了很多次,其中每一次的数据,就需要用 Captures 来取了

读书人网 >C#

热点推荐