一个有趣的C#正则替换问题

9.1(2) ------ 9.11
9(3).5(1) ----- 999.5
89(2).7 ------ 899.7

即把括号替换成括号前一数字的N次

原帖见http://topic.csdn.net/u/20100317/19/e25694b8-ff80-427a-910b-04a666ce6cbc.html

 

        static void Main(string[] args)
        {
            string[] input = { "9.1(2)", "9(3).5(1)", "89(2).7" };
            Regex reg = new Regex(@"(\d)\((\d+)\)");
            foreach (string s in input)
            {
                Console.WriteLine("源字符串:" + s.PadRight(15, ' ') + "   替换结果:" + reg.Replace(s, delegate(Match m) { return new string(m.Groups[1].Value[0], Convert.ToInt32(m.Groups[2].Value)); }) + "\n");
            }
        }
posted @ 2010-03-17 23:27  RobinLao  阅读(1085)  评论(0编辑  收藏  举报