C#正则表达式 Regex.Matches 使用变量条件
如有字符串: 广州:20深圳:755肇庆:758湛江:759 ......
string CityName = "广州" 或 "深圳" 或 "肇庆"...
如何使用 正则表达式 Regex.Matches 把变量 CityName 作条件时,
把它们的对应值找出来?
如:CityName = "广州" 时,值为:20. C#?正则表达式?Matches 正则表达式 c#
[解决办法]
string str = "广州:20深圳:755肇庆:758湛江:759";
string CityName = "广州";
var list = Regex.Matches(str, string.Format(@"(?<={0}:)\d+", CityName)).OfType<Match>().Select(t => t.Value).ToList();
string s = "广州:20深圳:755肇庆:758湛江:759";
Dictionary<string, string> dictionary = Regex.Matches(s, @"(?<city>[^:]+):(?<value>\d+)").Cast<Match>().ToDictionary(k => k.Groups["city"].Value, v => v.Groups["value"].Value);
Console.WriteLine(dictionary["深圳"]);//输出“深圳”的值