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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章