正则表达式学习,使用.NET framework api

View Code 
public static string Process(string input)
        {
            Regex r 
= new Regex(@"\$\{(?<custId>\d+):(?<attrib>\w+)}");
            
return r.Replace(input, Evaluate);
        }

        
static string Evaluate(Match match)
        {
            Customer c 
= new Customer { Name = "同志们", Ssn = "123", Company = "公司" };
            
switch (match.Groups["attrib"].Value)
            {
                
case "name":
                    
return c.Name;
                
case "ssn":
                    
return c.Ssn;
                
case "company":
                    
return c.Company;
                
default:
                    
throw new Exception();
                    
break;
            }
        }

        
class Customer
        {
            
public string Name { getset; }
            
public string Ssn { getset; }
            
public string Company { getset; }
posted @ 2011-04-01 14:04  年轻国王  阅读(181)  评论(0编辑  收藏  举报