2012-02-10 146 views
5

什么是PHP的preg_quote的等价物?什么是PHP的preg_quote的等价物?

这是据我已经得到了用于创建从一个字符串中提取文本的方法:

public static string f_get_string_between(string text, string start, string end) 
    { 
     //both these attempts below throw an unrecognized escape sequence error 
     //start = "\Q"+start+"\E"; 
     //end = "\Q"+end+"\E"; 

     Regex regex = new Regex(start + "(.*?)" + end); 
     var v = regex.Match(text); 
     text = v.Groups[1].ToString(); 
     return text; 
    } 

回答

7
+0

Regex.Escape很接近,但在某些情况下它似乎略有不同。特别是,它不会逃避可能成为问题或可能不成问题的“]”字符。 – 2012-02-10 21:59:58

0

有在C#中不直接替换preg_quote但你可以编写自己的函数来做到这一点。从PHP手册中,函数转义的字符是:. \ + * ? [^] $ () { } = ! < > | : -,因此您只需编写一个接受字符串的函数并转义这些字符。