正则学习:组的定义及引用方式
点击次数:23 次 发布日期:2008-11-26 12:25:18 作者:源代码网
|
源代码网推荐 把一次Match结果用(?<name>)的方式分成组,例子: 源代码网推荐 public static void Main() 源代码网推荐 { 源代码网推荐 string s = "2005-2-21"; 源代码网推荐 Regex reg = new Regex(@"(?<y>d{4})-(?<m>d{1,2})-(?<d>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); 源代码网推荐 Console.WriteLine(time); 源代码网推荐 Console.ReadLine(); 源代码网推荐 } 源代码网推荐 也可以根据正则里面()的顺序,使用编码访问组.第一个括号对包涵的组被自动编号为1,后面的括号依次编号为2、3…… 源代码网推荐 访问方式:match.Groups[1].Value 源代码网推荐 源代码网推荐 另外也可以用(?<数字>)的方式手工给每个括号对的组编号 源代码网推荐 源代码网推荐 苦闷的是如果过一段时间不使用正则的话,里面的符号很容易就忘记了,:-) 源代码网推荐 源代码网推荐 http://www.cnblogs.com/waitu/archive/2006/08/31/491192.html 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
