下面,图老师小编带您去了解一下正则学习:组的定义及引用方式,生活就是不断的发现新事物,get新技能~
【 tulaoshi.com - Web开发 】
  一个正则表达式匹配结果可以分成多个部分,这就是组(Group).
  把一次Match结果用(?name)的方式分成组,例子:
  public static void Main() 
              { 
               string s = "2005-2-21"; 
               Regex reg = new   Regex(@"(?yd{4})-(?md{1,2})-(?dd{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); 
               Console.WriteLine(time); 
               Console.ReadLine(); 
              }
  也可以根据正则里面()的顺序,使用编码访问组.第一个括号对包涵的组被自动编号为1,后面的括号依次编号为2、3…… 
  访问方式:match.Groups[1].Value
另外也可以用(?数字)的方式手工给每个括号对的组编号
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)苦闷的是如果过一段时间不使用正则的话,里面的符号很容易就忘记了,:-)
http://www.cnblogs.com/waitu/archive/2006/08/31/491192.html
来源:http://www.tulaoshi.com/n/20160219/1616476.html
看过《正则学习:组的定义及引用方式》的人还看了以下文章 更多>>