正则表达式学习,使用.NET framework api
时间:2011-04-01 来源:mrxliu
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 { get; set; }
public string Ssn { get; set; }
public string Company { get; set; }
{
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 { get; set; }
public string Ssn { get; set; }
public string Company { get; set; }
相关阅读 更多 +