字符串A中包含一些表示图片的字符串.
要将A中的这些字符串替换成包含Html标志的字符串要怎么写?比如:
string a=[img_0]???[img_1]???[IMG_N]
string b=Regex.Replace(???????? 这里应该怎么写?);
结果:
b=<IMG src="0.gif">???<img src="1.gif">???<IMG src="N.gif">

解决方案 »

  1.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    string s=@"[img_0]???[img_1]???[IMG_N]";
    string myreg=@"\[img_\w?\]";  //[和]要转义
    string a=Regex.Replace(s,myreg,new MatchEvaluator(this.mydo),System.Text.RegularExpressions.RegexOptions.IgnoreCase);
               this.textBox1.Text=a;
    } private string mydo(Match m)
    {
                string a=m.Value;
     
    string b=a.Substring(5,a.Length-6);
    string c="<IMG src=\""+b+".gif\">";
    return c;
    }
      

  2.   

    http://www.regexlib.com/DisplayPatterns.aspx?cattabindex=5&categoryId=5
      

  3.   

    string yourStr = "...";
    string regexStr = @"\[[^\]]+_([^\]]+)\]";
    string replacedStr = "<img src=\"$1.gif\">";string afterRepStr = Regex.Replace(yourStr, regexStr, replacedStr);