有一种方法可以将数据值转换为正则表达式模式中定义的另一个数据值吗?
我的意思是,我想定义一个类似 a=1|b=2|c=3 的模式。
因此,如果我将 a 值传递给 Regex,它会返回 1。如果 b 返回 2 ... 等等
这可能吗?
请您参考如下方法:
Dictionary<string, int> dic = new Dictionary<string, int>();
foreach (Match m in Regex.Matches("a=1|b=2|c=3", @"\w?=\d?"))
{
string[] val = m.Value.Split('=');
dic.Add(val[0], Int32.Parse(val[1]));
}
或者
string val = "a";
Int32.Parse(Regex.Match("a=1|b=2|c=3", val + @"=(\d)").Groups[1].Value);
