string text = "英格兰{#比分1#}击败了斯洛文尼亚,加纳{#比分2#}负于德国";
 
请问怎样用正则将 {# #} 里的 比分1 和 比分2 取出来呢,
并且使用新的值替换里面的内容,替换之后的结果为:string text = "英格兰1比0击败了斯洛文尼亚,加纳0比1负于德国";谢谢。

解决方案 »

  1.   

    System.Text.RegularExpressions.Regex.Replace("{#}", @"\{[^\}]*\}", "");
      

  2.   

    对应关系怎么样的?string text = "英格兰{#比分1#}击败了斯洛文尼亚,加纳{#比分2#}负于德国";
    Hashtable hs = new Hashtable();
    hs.Add("比分1", "1比0");
    hs.Add("比分2", "0比1");
    Regex reg = new Regex(@"\{#(比分\d+)#}");
    string result = reg.Replace(text, delegate(Match m) { return hs[m.Groups[1].Value].ToString(); });
    richTextBox2.Text = result;
      

  3.   

                string text = "英格兰1比0击败了斯洛文尼亚,加纳0比1负于德国";
                Regex reg = new Regex(@"(?is)英格兰(?<score1>\d{1,2}比\d{1,2}).*斯洛文尼亚.*加纳(?<score2>\d{1,2}比\d{1,2}).*德国");
                Match m = reg.Match(text);
                if (m.Success)
                {
                    Console.WriteLine(m.Groups["score1"].Value);
                    Console.WriteLine(m.Groups["score2"].Value);
                }
      

  4.   

    如果用Format可以么?正好看看那个接口。
      

  5.   

    string text = "英格兰{#比分1#}击败了斯洛文尼亚,加纳{#比分2#}负于德国";
    Hashtable hs = new Hashtable();
    hs.Add("比分1", "1比0");
    hs.Add("比分2", "0比1");
    Regex reg = new Regex(@"\{#(比分\d+)#}");
    string result = reg.Replace(text, delegate(Match m) { return hs[m.Groups[1].Value].ToString(); });
    richTextBox2.Text = result;
      

  6.   

    看来IFormatProvider不行,只能对格式修饰,不能把{0}也自定义。
      

  7.   

    如果你的格式可以稍微修改一点。来个IFormatProvider实现的。using System;namespace CSharpConsole01
    {
        public partial class Program
        {
            [STAThreadAttribute]
            static void Main(string[] args)
            {
                string text = "英格兰{0:score1}击败了斯洛文尼亚,加纳{0:score2}负于德国";
                text = string.Format(new ScoreFormater(), text, "1比0", "0比1");
                Console.WriteLine(text);
                Console.ReadKey();
            }        public class ScoreFormater : IFormatProvider, ICustomFormatter
            {
                public string Format(string format, object arg, IFormatProvider formatProvider)
                {
                    return arg.ToString();
                }            public object GetFormat(Type formatType)
                {
                    return formatType == typeof(ICustomFormatter) ? this : null;
                }
            }
        }
    }
      

  8.   

    string text = "英格兰{#比分1#}击败了斯洛文尼亚,加纳{#比分2#}负于德国";
    string a1 = null;
    string a2 = null;
    a1 = text.split(",")(0);
    a2 = text.split(",")(1);
    for (int i = 0; i <= 30; i++) {
    a1 = a1.replace("比分" + i, i + "比0");
    a2 = a2.replace("比分" + i, "0比" + i);
    }
    Console.WriteLine((a1 + "," + a2).Replace("(#", "").Replace("#)", ""));
      

  9.   

    string text = "英格兰{#比分1#}击败了斯洛文尼亚,加纳{#比分2#}负于德国";
    string a1 = text.Split(",")[0];
    string a2 = text.Split(",")[1];
    for (int i = 0; i <= 30; i++) {
    a1 = a1.Replace("比分" + i, i + "比0");
    a2 = a2.Replace("比分" + i, "0比" + i);
    }
    Interaction.MsgBox((a1 + "," + a2).Replace("{#", "").Replace("#}", ""));
      

  10.   


    Regex.Replace(input, "{#([^#{}]+)#}", "$1");
    //结果:英格兰比分1击败了斯洛文尼亚,加纳比分2负于德国