c#正则应用

 public   string   regexDate(string   str)  
  {  
  return   Regex.Replace(str,"'(?<year>//d{4})-(?<month>//d{1,2})-(?<day>//d{1,2})'","To_Date('${year}-${month}-${day}','yyyy-mm-dd')",RegexOptions.Multiline);     alimamal.php?i=mm_10498610_623104_1064237&u=http%3A%2F%2Fwww.kupoa.cn%2Fshow.aspx%3Fid%3D739%26cid%3D84&w=336&h=280&re=1024x768&sz=36&r=http%3A%2F%2Fwww.baidu.com%2Fs%3Fie%3Dgb2312%26bs%3Diis%25BC%25D9%25CB%25C0%26sr%3D%26z%3D%26cl%3D3%26f%3D8%26wd%3Dc%2523%25D5%25FD%25D4%25F2%25CC%25E6%25BB%25BB%26ct%3D0&cg=9ddbfee5c57002ddac34d643848d4052&prl=65958572&cas=prl&cah=738&caw=1024&ccd=32&ctz=8&chl=0&cja=1&cpl=0&cmm=0&cf=9.0&sx=350&sy=420&cbw=1006&cbh=1348
  }  

 

有些时候我偶们希望在正则表达式的替换中对替换的字符换进行简单的处理,比如把所有的A依次替换为B1、B2、B3……这就需在替换时对字符串进行处理,其实这个很简单,用C#中的MatchEvaluator委托就可以了。简单的示例如下:

引用内容:
    private static int i = 0;

    public static string ParseToHTML(string ubbString)
    {
        Regex rgx;
        string htmlString = "";

        MatchEvaluator me = new MatchEvaluator(AddOne);
        rgx = new Regex(@"/[code/](.*?)/[//code/]");
        htmlString = rgx.Replace(htmlString, me);

        return htmlString;
    }

    public static string AddOne(Match m)
    {
        string code = m.Value.Substring(6, m.Value.Length - 13);
        string codeString = @"<textarea name='code" + i + "' class='code_text'>" + code + "</textarea></div><br />";
        i++;
        return codeString;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章