I would like to modify the value of the "data source" component in a connection string. I'm thinking of the following solution:
Using this regex pattern:
"data source\=((\w|\-)+?\\{1}\w+?)\;"
I can obtain the following string matches:
Match.Groups[0].Value = "data source=MY-PC\SQLEXPRESS;"
Match.Groups[1].Value = "MY-PC\SQLEXPRES"
So in the connection string firstly I would like to find the part matching with the "data source=something;", and secondly replace just the "something" in the connection string. How to do that?
If you insist Regex replacing, please note C# cannot modify a built string, you need to get a new string with the needed part replaced.
var connectionString = @"data source=MY-PC\SQLEXPRESS;";
var pattern = @"(data source=)((\w|\-)+?\\\w+?)\;";
var newConnectionString = Regex.Replace(connectionString, pattern, "$1" + "something");
Console.WriteLine(newConnectionString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With