none
正则替换问题 RRS feed

  • 问题

  • 我要把a替换掉,但不能把[Pic]{8F6D8B04-0C95-492a-AAF5-41A8267C47AA}.bmp[/Pic]内的a和/:a4中的a替换,怎么弄呢?

    源码:abcd[Pic]{8F6D8B04-0C95-492a-AAF5-41A8267C47AA}.bmp[/Pic]/:a4abcd


    2011年8月1日 6:08

答案

  • 我要把a替换掉,但不能把[Pic]{8F6D8B04-0C95-492a-AAF5-41A8267C47AA}.bmp[/Pic]内的a和/:a4中的a替换,怎么弄呢?

    源码:abcd[Pic]{8F6D8B04-0C95-492a-AAF5-41A8267C47AA}.bmp[/Pic]/:a4abcd


    void Main()
    {
    	 string str = "abcd[Pic]{8F6D8B04-0C95-492a-AAF5-41A8267C47AA}.bmp[/Pic]/:a4abcd";
    	 str=Regex.Replace(str,@"(?<!(/:)|(\[Pic\]{[^}]+))a","");
    	 Console.WriteLine(str);
    	 //bcd[Pic]{8F6D8B04-0C95-492a-AAF5-41A8267C47AA}.bmp[/Pic]/:a4bcd
    }
    





    Best Regards,
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    2011年8月1日 10:30

全部回复

  •  private void button1_Click(object sender, EventArgs e)
        {
          string str = "abcd[Pic]{8F6D8B04-0C95-492a-AAF5-41A8267C47AA}.bmp[/Pic]/:a4abcd";
          string parren =@"\[Pic\].+\[/Pic\]";
          MatchCollection mc1=Regex.Matches(str, parren, RegexOptions.IgnoreCase);
          ArrayList bb = new ArrayList();
          for (int i = 0; i < mc1.Count; i++)
         {
           str = Regex.Replace(str, parren, "bb[" + i.ToString() + "]");
         
           bb.Add(mc1[i].Value);
         }
         str = str.Replace("/:a", "@");
          //开始修改
         str = str.Replace("a"," ");
         MessageBox.Show(str);
    
          //还原
         str = str.Replace("@", "/:a");
    
         for (int i = 0; i <bb.Count ; i++)
         {
           str=str.Replace("bb["+i.ToString()+"]",bb[i].ToString());
         }
         MessageBox.Show(str);
        }
    


    http://blog.csdn.net/zx13525079024
    2011年8月1日 9:57
  • 我要把a替换掉,但不能把[Pic]{8F6D8B04-0C95-492a-AAF5-41A8267C47AA}.bmp[/Pic]内的a和/:a4中的a替换,怎么弄呢?

    源码:abcd[Pic]{8F6D8B04-0C95-492a-AAF5-41A8267C47AA}.bmp[/Pic]/:a4abcd


    void Main()
    {
    	 string str = "abcd[Pic]{8F6D8B04-0C95-492a-AAF5-41A8267C47AA}.bmp[/Pic]/:a4abcd";
    	 str=Regex.Replace(str,@"(?<!(/:)|(\[Pic\]{[^}]+))a","");
    	 Console.WriteLine(str);
    	 //bcd[Pic]{8F6D8B04-0C95-492a-AAF5-41A8267C47AA}.bmp[/Pic]/:a4bcd
    }
    





    Best Regards,
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    2011年8月1日 10:30