沒有測試,應該加入一些檢查,但是,看到的版本@Ulugbek Umirov的:)
var pattern = @"\[(.*?)\]";
var matches = Regex.Matches(Param, pattern);
foreach (Match m in matches) {
var inb = "[" + m.Groups[1] + "]";
var results = arrayval.Select (s => s.Split(':'))
.FindAll(ss => ss[0] == m.Groups[1])
.Select (ss => ss[1]);
Param = Param.Replace(inb, results[0]);
}
順便說一句... @Ulugbek Umirov的短一點的版本,應該只是罰款:)
Param = Regex.Replace(Param, @"\[(.+?)\]", m =>
arrayval.Select(s => s.Split(new[] { ':' }, 2))
.Where (p => p.Length == 2)
.Where (p => p[0] == m.Groups[1].Value)
.Select(p => p[1])
.FirstOrDefault() ?? m.Value);
更新與更換0如果不存在:
Param = Regex.Replace(Param, @"\[(.+?)\]", m =>
arrayval.Select(s => s.Split(new[] { ':' }, 2))
.Where (p => p.Length == 2)
.Where (p => p[0] == m.Groups[1].Value)
.Select(p => p[1])
.FirstOrDefault() ?? "0");
爲什麼不使用字典而不是數組? – Sagar