如何避免字符串在Boost Regex中使用?

[英]How to escape a string for use in Boost Regex


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

我只是在思考正則表達式,我正在使用Boost Regex庫。

I have a need to use a regex that includes a specific URL, and it chokes because obviously there are characters in the URL that are reserved for regex and need to be escaped.

我需要使用包含特定URL的regex,它會阻塞,因為顯然URL中有些字符是為regex保留的,需要轉義。

Is there any function or method in the Boost library to escape a string for this kind of usage? I know there are such methods in most other regex implementations, but I don't see one in Boost.

在Boost庫中是否有函數或方法來轉義這種用法的字符串?我知道在大多數regex實現中都有這樣的方法,但是我在Boost中看不到這樣的方法。

Alternatively, is there a list of all characters that would need to be escaped?

或者,是否有需要轉義的所有字符的列表?

4 个解决方案

#1


34  

. ^ $ | ( ) [ ] { } * + ? \

Ironically, you could use a regex to escape your URL so that it can be inserted into a regex.

諷刺的是,您可以使用regex來轉義URL,以便將其插入到regex中。

const boost::regex esc("[.^$|()\\[\\]{}*+?\\\\]");
const std::string rep("\\\\&");
std::string result = regex_replace(url_to_escape, esc, rep,
                                   boost::match_default | boost::format_sed);

(The flag boost::format_sed specifies to use the replacement string format of sed. In sed, an escape & will output whatever matched by the whole expression)

(標志boost:::format_sed指定使用sed的替換字符串格式。在sed中,escape &將輸出與整個表達式匹配的任何內容)

Or if you are not comfortable with sed's replacement string format, just change the flag to boost::format_perl, and you can use the familiar $& to refer to whatever matched by the whole expression.

或者,如果您不熟悉sed替換字符串格式,只需將標志更改為boost::format_perl,並可以使用熟悉的$&來引用與整個表達式匹配的任何內容。

const std::string rep("\\\\$&");
std::string result = regex_replace(url_to_escape, esc, rep,
                                   boost::match_default | boost::format_perl);

#2


12  

Using code from Dav (+ a fix from comments), I created ASCII/Unicode function regex_escape():

使用Dav(加上注釋中的修復)中的代碼,我創建了ASCII/Unicode函數regex_escape():

std::wstring regex_escape(const std::wstring& string_to_escape) {
    static const boost::wregex re_boostRegexEscape( _T("[.^$|()\\[\\]{}*+?\\\\]") );
    const std::wstring rep( _T("\\\\&") );
    std::wstring result = regex_replace(string_to_escape, re_boostRegexEscape, rep, boost::match_default | boost::format_sed);
    return result;
}

For ASCII version, use std::string/boost::regex instead of std::wstring/boost::wregex.

對於ASCII版本,使用std::string/boost:::regex而不是std::wstring/boost::wregex。

#3


4  

Same with boost::xpressive:

與提高::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;
}

#4


1  

In C++11, you can use raw string literals to avoid escaping the regex string:

在c++ 11中,您可以使用原始字符串文本來避免從regex字符串中轉義:

std::string myRegex = R"(something\.com)";

std::string myRegex = R”(\ com)”;

See http://en.cppreference.com/w/cpp/language/string_literal, item (6).

看到http://en.cppreference.com/w/cpp/language/string_literal項目(6)。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/08/10/72f51288418d8b0994d11898c9a54710.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com