正则表达式从“a”到“b”查找并替换字符串

时间:2017-08-29 13:57:18

标签: regex

如何更换“连接超时= 120;” “连接超时= 180;”的值从以下字符串?

data source = abc \ aaa; Packet Size = 8192; initial catalog = asdfvbfadf; Integrated Security = SSPI; Persist Security Info = False; Connection Timeout = 120;最大池大小= 1000; Enlist = true

该字符串将始终包含开头“Connection Timeout =”,并以第一个“;”

结束

2 个答案:

答案 0 :(得分:1)

这是一个使用C#的简单示例 如果这不是您所需要的,请在您的问题中添加编程语言标记。

string str = "data source=abc\\aaa;Packet Size=8192;initial catalog=asdfvbfadf;Integrated Security=SSPI;Persist Security Info=False;Connection Timeout=120; Max Pool Size = 1000;Enlist=true";
MessageBox.Show(System.Text.RegularExpressions.Regex.Replace(str, "(Connection Timeout)=\\d+", "$1=180"));

答案 1 :(得分:0)

由于您未提及编程语言,因此在javascript中给出了答案: public interface IService<T> { } public interface IResolveService { IService<T> Resolve<T>(); } public class ResolveService : IResolveService { private readonly IServiceProvider _provider; public ResolveService(IServiceProvider provider) { _provider = provider; } public IService<T> Resolve<T>() { //some logic here. May by resolving some instances using IServiceProvider throw new NotImplementedException(); } } public class MyClass { public MyClass(IResolveService resolveService) { Service = resolveService.Resolve<int>(); } private IService<int> Service { get; } } 只会替换120,newString1会替换任何数字

newString2