正则表达式中的组集合的使用

作者:互联网

2009-07-01

ASP.NET教程

简单实例:

    string s = "2005-2-21";
    Regex reg = new Regex(@"(?d{4})-(?d{1,2})-(?d{1,2})",RegexOptions.Compiled);
         
    Match match = reg.Match(s);
    int year =  int.Parse(match.Groups["y"].Value);
    int month = int.Parse(match.Groups["m"].Value);
    int day = int .Parse(match.Groups["d"].Value);
    DateTime time = new DateTime(year,month,day);

相关标签:

ASP.NET