We help IT Professionals succeed at work.

Regular Expression: To change connection string in web.config

443 Views
Last Modified: 2018-10-10
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

Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
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. :-)
CERTIFIED EXPERT

Commented:
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.
Fernando SotoRetired
CERTIFIED EXPERT
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
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

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

Warm Regards

Author

Commented:
Thank you very much. It works great.
Fernando SotoRetired
CERTIFIED EXPERT
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
CERTIFIED EXPERT
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.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.