例句:“中华人民共和国成立于1949年10月1日,I、U、R、#”
查找其中的“1949、10、1、I、U、R、#”
然后替换成“中华人民共和国成立于一九四九年十月一日,电流、电压、电阻、号”如何用一句正则式进行查找替换呢?正则

解决方案 »

  1.   


                System.IO.StreamReader reader1 = new System.IO.StreamReader("e:\\1.txt",Encoding.Default);
                string str = @"中华人民共和国成立于1949年10月1日,I、U、R、#";
                string strmatch = @"(?is)[\d]+|[a-z]|#";
                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(strmatch);
                System.Text.RegularExpressions.MatchCollection mc = reg.Matches(str);
                foreach (System.Text.RegularExpressions.Match mm in mc)
                {
                    MessageBox.Show(mm.Value);
                }
      

  2.   

    List<string> wordtomatch = new List<string>() { "1949", "10", "1", "I", "U", "R", "#" };
    List<string> replacewith = new List<string>() { "一九四九", "十", "一", "电流", "电压", "电阻", "号" };
    wordtomatch.ForEach(x => s = s.Replace(x, replacewith[wordtomatch.FindIndex(y => y == x)]));
      

  3.   

    谢谢bdmh,
    若将例句采用string str = Regex.Replace(TextBox1.txt, "查找1949、10、1、I、U、R、#", "替换一九四九、十、一、电流、电压、电阻、号");这样的正则式该如何写呢?
    TextBox2.txt=”中华人民共和国成立于一九四九年十月一日,电流、电压、电阻、号“
      

  4.   


    谢谢caozhy
    问题解决啦!也非常感谢bdmh!3Q!