正则里面截取的时候有个“|”,怎么不让她编译啊,“|”就是个字符。

解决方案 »

  1.   

    何止|,多得很。你都自己去转义?用系统的方法好了
    Regex.Escape(希望作为纯文本查找的字符串)
    例如,你希望找到*?|这三个字母连续的组合
    Regex reg = new Regex(Regex.Escape("*?|"));
    reg.Matches(....
      

  2.   

     string regString = string.Format("{0}(?<getcontent>[\\s|\\S]+?){1}", regStart, regEnd);Regex  reg = new Regex(regString); 
    我是想截取固定的一个字符串就是以regStart开头,以regEnd结尾的,结果regStart里面有“|”
      

  3.   

    static void Main(string[] args)
    {
        string yourHtml = @"开始文档结束";
        Regex reg = new Regex(GetTextBetween("开始", "结束",false));
        Console.WriteLine(reg.Match(yourHtml).Value);
        Console.ReadKey();
    }public static string GetTextBetween(string head, string end,bool include_mask)
    {
        if (include_mask)
        {
            return Regex.Escape(head) + @"[\s\S]*?" + Regex.Escape(end);
        }
        else
        {
            return "(?<=" + Regex.Escape(head) + @")[\s\S]*?(?=" + Regex.Escape(end) + ")";
        }            
    }