且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何转义用于Boost Regex的字符串

更新时间:2022-10-17 20:38:47

 。 ^ $ | ()[] {} * +?讽刺的是,您可以使用正则表达式来转义您的URL,以便将其插入到正则表达式。
  const boost :: regex esc([。^ $ |()\\ [\\ ] {} * + \\\\])?; 
const std :: string rep(\\\\&);
std :: string result = regex_replace(url_to_escape,esc,rep,
boost :: match_default | boost :: format_sed);

(标志 boost :: format_sed 指定使用sed的替换字符串格式在sed中,一个转义& 将输出与整个表达式匹配的任何值)



或者如果您对sed的替换字符串格式不舒服,只需将标志更改为 boost :: format_perl ,您就可以使用熟悉的 $& 指代与整个表达式匹配的任何内容。

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


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 chokes because obviously there are characters in the URL that are reserved for regex and need to be escaped.

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.

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

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

Ironically, you could use a regex to escape your URL so that it can be inserted into a 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)

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.

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