C#基础知识之 正则表达式
正则表达式用于提取字符串中的指定的内容
我们在工作中可能会在某一个字符串中提取出来其中某一指定部分的内容。
使用正则表达式无疑是最快的,且效率最高的。
代码
数据库链接字符串根据某个条件动态改变
var newDbName = string.Empty;
var dbName = @"database=(?<dbName>[\s\S]*?);";
Regex reg = new Regex(dbName, RegexOptions.IgnoreCase);
Match match = reg.Match(ConnectionString);
if (match.Success)
{
newDbName = match.Groups["dbName"].Value;
}
ConnectionString2 = ConnectionString.Replace(newDbName , "newDb");
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["ConnectionString2"].Value = ConnectionString2;
config.Save();
return GetDataSet(ConnectionString2, SQLString);