求字符串解析?
字符串如下:
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;也要去掉哈
[解决办法]
正则表达式不会写
通俗点的,可以用截取实现
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", "");
[解决办法]
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}