Match.Groups正则表达式的中的高级应用,理解了Match.Groups你就可以利用正则表达式操作得到更多想要得到的结果。

using System;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;

class TestMatchGroups
{

    public static void Main()
    {

        //一部
        Console.WriteLine(Regex.Match(@"Tell me who you are\Radys$1982#06", @".*\\(.*?)$").Groups[1]);
        //两部分
        Console.WriteLine("My name is:{0},and my year is:{1}",
                  Regex.Match(@"Tell me who you are\Radys$1982#06", @".*\\(?<myname>.*?)(?<year>\$.*?)").Groups["myname"],
                  Regex.Match(@"Tell me who you are\Radys$1982#06", @".*\\(?<myname>.*?)\$(?<year>.*?)$").Groups["year"]
            );
        //三个
        Console.WriteLine("My name is:{0},and my year is:{1},the month is:{2}",
          Regex.Match(@"Tell me who you are\Radys$1982#06", @".*\\(?<myname>.*?)(?<year>\$.*?)").Groups["myname"],
          Regex.Match(@"Tell me who you are\Radys$1982#06", @".*\\(?<myname>.*?)\$(?<year>.*?)$").Groups["year"],
          Regex.Match(@"Tell me who you are\Radys$1982#06", @".*\\(?<myname>.*?)\$(?<year>.*?)#(?<month>.*?)$").Groups["month"]
    );
 }

}

posted on 2008-12-01 23:50  阳春  阅读(1210)  评论(0)    收藏  举报