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);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다