//类似的字符串 
String Str="ABCD<PicImage| 429E32838642EA9057BACD856/>
EFG<PicImage| 85E32838622EA9057BACD856/>
123<PicImage| 85E32838622EA9057BACD856/>";如何截取存在<PicImage xxxxxxxxx |/>中的字符串进行保存,
并且把<PicImage xxxxxxxxx |/> 替换成 AAA ?

解决方案 »

  1.   

    String Str = @"ABCD<PicImage| 429E32838642EA9057BACD856/>
    EFG<PicImage| 85E32838622EA9057BACD856/>
    123<PicImage| 85E32838622EA9057BACD856/>";
                Str = Regex.Replace(Str, @"<PicImage[^>]*>", "AAA");
               
      

  2.   


    String Str = "ABCD<PicImage| 429E32838642EA9057BACD856/>EFG<PicImage| 85E32838622EA9057BACD856/>123<PicImage| 85E32838622EA9057BACD856/>";
    List<string> list = new List<string>(); //保存到list中
    Regex reg = new Regex(@"(?i)<PicImage\|\s*([^/]+)/>");
    string result = reg.Replace(Str, delegate(Match m){ list.Add(m.Groups[1].Value); return "AAA";}); 
    richTextBox2.Text = "替换结果:" + result + "\n保存内容:\n";
    foreach (string s in list)
    {
        richTextBox2.Text += s + "\n";
    }
    /*-----输出-----
    替换结果:ABCDAAAEFGAAA123AAA
    保存内容:
    429E32838642EA9057BACD856
    85E32838622EA9057BACD856
    85E32838622EA9057BACD856
    */
      

  3.   

    版主大神,我还想在每次截取时list.Add(m.Groups[1].Value);中在获取ABCD这些值,怎么办
      

  4.   


    都放在一个list中?
    String Str = "ABCD<PicImage| 429E32838642EA9057BACD856/>EFG<PicImage| 85E32838622EA9057BACD856/>123<PicImage| 85E32838622EA9057BACD856/>";
    List<string> list = new List<string>();
    Regex reg = new Regex(@"(?i)([^\s<]+)<PicImage\|\s*([^/]+)/>");
    string result = reg.Replace(Str, delegate(Match m) { list.Add(m.Groups[1].Value); list.Add(m.Groups[2].Value); return "AAA"; });
    richTextBox2.Text = "替换结果:" + result + "\n保存内容:\n";
    foreach(string s in list)
    {
        richTextBox2.Text += s + "\n";
    }
    /*-----输出-----
    替换结果:AAAAAAAAA
    保存内容:
    ABCD
    429E32838642EA9057BACD856
    EFG
    85E32838622EA9057BACD856
    123
    85E32838622EA9057BACD856
    */
      

  5.   

    ABCD<PicImage| 429E32838642EA9057BACD856/> 
    2个list 
    一个放ABCD
    一个放429E32838642EA9057BACD856