Mit*_*áti 3 .net c# regex connection-string
我想修改连接字符串中“数据源”组件的值。我正在考虑以下解决方案:
使用这个正则表达式模式:
"data source\=((\w|\-)+?\\{1}\w+?)\;"
Run Code Online (Sandbox Code Playgroud)
我可以获得以下字符串匹配:
Match.Groups[0].Value = "data source=MY-PC\SQLEXPRESS;"
Match.Groups[1].Value = "MY-PC\SQLEXPRES"
Run Code Online (Sandbox Code Playgroud)
因此,首先在连接字符串中,我想找到与“数据源=某事;”匹配的部分,其次仅替换连接字符串中的“某事”。怎么做?
如果您坚持使用 Regex 替换,请注意 C# 无法修改构建的字符串,您需要获取替换所需部分的新字符串。
var connectionString = @"data source=MY-PC\SQLEXPRESS;";
var pattern = @"(data source=)((\w|\-)+?\\\w+?)\;";
var newConnectionString = Regex.Replace(connectionString, pattern, "$1" + "something");
Console.WriteLine(newConnectionString);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8058 次 |
最近记录: |