源码:  /*---------------生成静态页-------------------*/
                Hashtable ht = new Hashtable();
                ht.Add("$con$", info.ProductIntro);
                ht.Add("$title$", info.PageTitle);
                foreach (DictionaryEntry de in varTable)
               {
                //此处应该如何写表达式把$con$替换成info.ProductIntro的内容,把$title$替换成info.pagetitle的内容
               }
    

解决方案 »

  1.   

    不明白你的意思,你是要怎样替换?把 de.Value 里的 $XXX$ 全部换成 ht 里对应的内容?
      

  2.   

    我的意图如下:
    string str="<html><head><title>$title$</title><head><body>$content$</body>"
    str为原内容.现在通过hashtable把要替换的字符和替换的内容引进来.然后在循环里面把str字符串对应de.key的内容替换成de.valve假如下代码:
    string str="<html><head><title>$title$</title><head><body>$content$</body>"
    string content="";
    Hashtable ht = new Hashtable(); 
                    ht.Add("$con$", info.ProductIntro); 
                    ht.Add("$title$", info.PageTitle); 
                    foreach (DictionaryEntry de in varTable) 
                  { 
                      content=regx.replace(str,.....)
                    //省略号的部分如何写?
                  } 
      

  3.   

    不用foreach 
     foreach了也会报错的
    Hashtable 是键值对格式
       直接这样写就行了
    ht["$con$"]=info.ProductIntro;
    Hashtable 添加相同键名的时候,他会把这个键值给覆盖了
      

  4.   

    是不是这个意思
     string str = " <html> <head> <title>$title$</title> <head> <body>$content$</body>";
                string content=""; 
                Hashtable ht = new Hashtable(); 
                ht.Add("$title$", "aaaaaa"); 
                ht.Add("$content$", "bbbbbb");
                foreach (DictionaryEntry de in ht) 
                { 
                     str = str.Replace(de.Key.ToString(),de.Value.ToString());
                } 
      

  5.   

    没必要用循环,假设正则表达式是 $\w+$,直接用
    reg.Replace(str, delegate(Match m){return ht[m.Value] as string})
    就可以了。
    第二个参数是一个委托,会把匹配到的值传入到该委托,然后这个方法用委托返回的值替换那个匹配到的字串。
      

  6.   

    对.就是这个意思.但str.replace不支持循环.所以才想到用正则. 
      

  7.   


    string str = " <html> <head> <title>$title$ </title> <head> <body>$content$ </body>";
    string content="";
    System.Collections.Hashtable ht = new System.Collections.Hashtable(); 
    ht.Add("$content$", "內容"); 
    ht.Add("$title$", "标题");
    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"\$\w+\$");
    content = reg.Replace(str, r => ht[r.Value] as string);
    Console.WriteLine(content); //显示:<html> <head> <title>标题 </title> <head> <body>内容 </body>应该是这个意思吧?
      

  8.   

    str.replace方法是不能循环的.一个一个替换又显得太麻烦.如果要替 换的地方有二十处 .要写二十次replace().而且还不通用.所以想用正则的方法来解决.但又不知道如何写
      

  9.   

     string str = " <html> <head> <title>$title$</title> <head> <body>$title$$content$</body>";
            Hashtable ht = new Hashtable();
            ht.Add("$title$", "aaaaaa");
            ht.Add("$content$", "bbbbbb");
            foreach (DictionaryEntry de in ht)
            {
                str=Regex.Replace(str, de.Key.ToString().Replace("$","\\$"), de.Value.ToString());
            }
      

  10.   


    这个没太看懂.这个\W应该会把类似于$aa$也替换掉的吧