日期:2014-05-18 浏览次数:20907 次
string s = "UserName:zhangsan#UserPwd:test#ID:1#Address:湖南"; protected void Page_Load(object sender, EventArgs e) { Response.Write(ABC("Address")); } private string ABC(string srcStr) { return Regex.Match(s, string.Format(@"(?i)(?:^|#){0}:([^#]+)", srcStr)).Groups[1].Value; }
------解决方案--------------------
private static string ReturnValue(string srcStr) { var html = @"UserName:zhangsan#UserPwd:test#ID:1#Address:湖南"; return Regex.Match(html, @"(?i)([^:#]*)" + srcStr + ":([^:#]*)").Groups[2].Value; } static void Main(string[] args) { Console.WriteLine(ReturnValue("UserName")); Console.WriteLine(ReturnValue("UserPwd")); Console.WriteLine(ReturnValue("ID")); Console.WriteLine(ReturnValue("Address")); }