How to escape a string for use in Boost Regex

前端 未结 4 1892
粉色の甜心
粉色の甜心 2020-11-27 17:21

I\'m just getting my head around regular expressions, and I\'m using the Boost Regex library.

I have a need to use a regex that includes a specific URL, and it choke

4条回答
  •  天命终不由人
    2020-11-27 17:49

    Same with boost::xpressive:

    const boost::xpressive::sregex re_escape_text = boost::xpressive::sregex::compile("([\\^\\.\\$\\|\\(\\)\\[\\]\\*\\+\\?\\/\\\\])");
    
    std::string regex_escape(std::string text){
        text = boost::xpressive::regex_replace( text, re_escape_text, std::string("\\$1") );
        return text;
    }
    

提交回复
热议问题