Regular Expression: To change connection string in web.config

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
LVL 6
MohitPanditAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Terry WoodsIT GuruCommented:
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. :-)
0
wilcoxonCommented:
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

0
MohitPanditAuthor Commented:
Alright, thanks. I'll work on it and let you know.
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Fernando SotoRetiredCommented:
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

0
MohitPanditAuthor 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
0
Fernando SotoRetiredCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MohitPanditAuthor Commented:
alright, thanks much. Let me check & let you know.

Warm Regards
0
MohitPanditAuthor Commented:
Thank you very much. It works great.
0
Fernando SotoRetiredCommented:
Not a problem MohitPandit, glad I was able to help. 👍
0
MohitPanditAuthor 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
0
Fernando SotoRetiredCommented:
Do not understand. Can you give examples of before and after? Also please open a new question for this. Thank you.
0
MohitPanditAuthor Commented:
Yes, I've given example. Okay, let me open new one.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.