Regular Expression: To change connection string in web.config

MohitPandit
MohitPandit used Ask the Experts™
on
Hi Folks,

Could you assist me to generate regular expression to change web.config connection string?
For example, below "DBString", I need to make change in server, user id, password like servername, userid password values respectively
<add name="DBString" connectionString="server=servername;database=DBName;user id=userid;password=password;Application Name=Web:TestApp" providerName="System.Data.SqlClient"/>

Open in new window


Could you assist into it?

Best Regards
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Terry WoodsIT Guru
Most Valuable Expert 2011

Commented:
I'm not sure the .NET code for it without Googling it, but I do know you can do a regex replace with .NET.

I can help with the pattern and replacement. You'd use a pattern like this to replace the servername (you may need to escape the double quotes):
(<add name="DBString" connectionString="[^"]*?server=)[^;]+

Open in new window

with a replacement:
$1newservername

Open in new window


The $1 part of the replacement ensures that the <add... part of the string doesn't get removed during the replacement.

Then just re-run the replacement for each value and change the variable names and values as required, eg change "server" in the pattern to "user id" and change "newservername" in the replacement to the new user id.

Back up your data first, as obviously I don't offer a guarantee. :-)
I'm also not sure of the .NET code but this regex and replacement should work as well:

(?<=server=)\w+(;database=DBname;user id=)\w+(;password=)\w+

Open in new window


newservername${1}newuserid${2}newpassword

Open in new window

Author

Commented:
Alright, thanks. I'll work on it and let you know.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi MohitPandit;

Here is a C# code snippet to replace the needed items in the connection string.
string pattern = "(?<1>server=)(?<2>[^;]+)(?<3>.*user id=)(?<4>[^;]+)(?<5>.*password=)(?<6>[^;]+)(?<7>.*)";
string server = "MyServer";
string userId = "MyUserName";
string pw = "MyPW";
string replacement = "$1" + server + "$3" + userId + "$5" + pw + "$7";
string oldConnStr = "<add name=\"DBString\" connectionString=\"server=servername;database=DBName;user id=userid;password=password;Application Name=Web:TestApp\" providerName=\"System.Data.SqlClient\"/>";

string newConnStr = Regex.Replace(oldConnStr, pattern, replacement);

Open in new window

Author

Commented:
Thank you Fernando Soto. It looks great.
Just know, I came to know other thing in connection string i.e. I can have two types of connection string which should be replace by reg ex. Could you assist on that too in single reg ex only? Please note, I can also have Data Source, Initial Catalog, uid, pwd


Old one:
<add name="DBString" connectionString="server=servername;database=DBName;user id=userid;password=password;Application Name=Web:TestApp" providerName="System.Data.SqlClient"/>


Other connection string
<add name="DBString" connectionString="Data Source=servername;Initial Catalog=DBName;uid=userid;pwd=password;Application Name=Web:TestApp" providerName="System.Data.SqlClient"/>

Best Regards
Retired
Distinguished Expert 2017
Commented:
Hi MohitPandit;

This will take care of any combination of the two connection strings format.
string pattern = "(.*(server|Data Source)=)([^;]+)(.*(database|Initial Catalog)=)([^;]+)(.*(user id|uid)=)([^;]+)(.*(password|pwd)=)([^;]+)(.*)";
string server = "MyServer";
string dbName = "MyNewDB";
string userId = "MyUserName";
string pw = "MyPW";
string replacement = "$1" + server + "$4" + dbName + "$7" + userId + "$10" + pw + "$13";
// Connection string in one of the two formats
string oldConnStr = "<add name=\"DBString\" connectionString=\"server=servername;database=DBName;user id=userid;password=password;Application Name=Web:TestApp\" providerName=\"System.Data.SqlClient\"/>";

string newConnStr = Regex.Replace(oldConnStr, pattern, replacement);

Open in new window

Author

Commented:
alright, thanks much. Let me check & let you know.

Warm Regards

Author

Commented:
Thank you very much. It works great.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not a problem MohitPandit, glad I was able to help. 👍

Author

Commented:
Hi Fernando Soto,

Sorry to pester you again, as per aforesaid two strings i.e. "Old one:"  & "Other connection string"
Assume, we have static value in for name key like "DBString1" and "DBString2" as per below

<add name="DBString1" connectionString="server=servername;database=DBName;user id=userid;password=password;Application Name=Web:TestApp" providerName="System.Data.SqlClient"/>

Open in new window


<add name="DBString2" connectionString="Data Source=servername;Initial Catalog=DBName;uid=userid;pwd=password;Application Name=Web:TestApp" providerName="System.Data.SqlClient"/>

Open in new window


Could you create two matching pattern like I mentioned aforesaid "DBString1" and "DBString2" would not replace?
In case "DBString1" match then replace with different values.
In case "DBString2" match then replace with different values.

Best Regards
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Do not understand. Can you give examples of before and after? Also please open a new question for this. Thank you.

Author

Commented:
Yes, I've given example. Okay, let me open new one.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial