代码如下:string str1="1(3,2)|2(2,1)"; 现在想要把括号里面的数字替换,3替换成0,2替换成1,1替换成3; 最终结果是 str1="1(0,1)|2(1,3)";
各位大侠帮帮忙!! 

解决方案 »

  1.   

     string str1 = "1(3,2)|2(2,1)";
                string str = Regex.Replace(str1, @"(?<=\()\d+(,\d+)*(?=\))", delegate(Match m)
                {
                    Func<string, string> fun = (t) =>
                    {
                        switch (t)
                        {
                            case "1": return "3";
                            case "2": return "1";
                            case "3": return "0";
                            default: return t;
                        }
                    };
                    return string.Join(",", m.Value.Split(',').Select(tt => fun(tt)).ToArray());
                });
                Console.WriteLine(str);
      

  2.   

    你多套几层Replace就行了,不过2楼的方法似乎很犀利哦,可以试试
      

  3.   


    string str1 = "1(3,2)|2(2,1)";
    Regex reg = new Regex(@"(?<=\((?:\d+,)*)\d+(?=(?:,\d+)*)");
    string result = reg.Replace(str1, delegate(Match m) { switch (m.Value) { case "3": return "0"; case "2": return "1"; case "1": return "3"; } return m.Value; });
    richTextBox2.Text = result;
      

  4.   

    string str1="1(3,2)|2(2,1)";
    string result =str1.Replace("3,2","0,1").Replace("2,1","1,3");