string constr = "data source=127.0.0.1;uid=sa;pwd=sa;database=pub;";
请问怎么从上面的字符串里抠出ip,用户,密码和数据库名?正则该怎么写?很迷糊

解决方案 »

  1.   

    string s = "data source=127.0.0.1;uid=sa;pwd=sa;database=pub;";
    string r = Regex.Match(s, @"database=([^;]+)").Groups[1].Value;
    Response.Write(r);
    只要把database改成别的就可以取出各个部分。
      

  2.   

    看我的回答http://topic.csdn.net/u/20121030/23/d77558ff-d65a-4be9-8a04-4b1983509de1.html
      

  3.   

    这个比我1楼的更好,直接通过Dictionary的键值获取对应值:
    string s = "data source=127.0.0.1;uid=sa;pwd=sa;database=pub;";
    Dictionary<string, string> dictionary = s.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToDictionary(x => x.Split('=')[0], x => x.Split('=')[1]);
    foreach (KeyValuePair<string, string> pair in dictionary)
    Console.WriteLine(pair.Key + ":" + pair.Value);
    Console.WriteLine("uid的值是:" + dictionary["uid"]);
      

  4.   

    感谢大家帮忙,今天一晚都在倒弄BulkCopy,总算有所收获