日期:2014-05-17  浏览次数:20503 次

求字符串解析?
字符串如下:
ConnectString={Persist Security Info=True;Provider=SQLOLEDB.1;User ID=sa;Password=123456;Data Source=XXXXX;Initial Catalog=DB8888};UserName=XX;UserID=XX;

如何得到以下字符串
Persist Security Info=True;User ID=sa;Password=123456;Data Source=XXXXX;Initial Catalog=DB8888

注意:Provider=SQLOLEDB.1;也要去掉哈

------解决方案--------------------
正则表达式不会写
通俗点的,可以用截取实现
C# code

string str = "ConnectString={Persist Security Info=True;Provider=SQLOLEDB.1;User ID=sa;Password=123456;Data Source=XXXXX;Initial Catalog=DB8888};UserName=XX;UserID=XX;";
            string str1 = str.Substring(str.IndexOf('{'));
            string str2 = str1.Substring(0, str1.IndexOf('}')).Replace("Provider=SQLOLEDB.1", "");

------解决方案--------------------
探讨
正则表达式不会写
通俗点的,可以用截取实现

C# code

string str = "ConnectString={Persist Security Info=True;Provider=SQLOLEDB.1;User ID=sa;Password=123456;Data Source=XXXXX;Initial Catalog=DB8888};UserName=XX;UserID……

------解决方案--------------------
C# code

void Main()
{
    string str=@"ConnectString={Persist Security Info=True;Provider=SQLOLEDB.1;User ID=sa;Password=123456;Data Source=XXXXX;Initial Catalog=DB8888};UserName=XX;UserID=XX;";
    string result=Regex.Replace(Regex.Match(str,@"(?<={)[^}]+(?=})").Value,@"(?i)Provider[^;]+;","");
    Console.WriteLine(result);
    //Persist Security Info=True;User ID=sa;Password=123456;Data Source=XXXXX;Initial Catalog=DB8888
}