问题描述
我正在使用正则表达式,我正在使用Boost Regex库。
我需要使用一个正则表达式,包括一个特定的URL,并且它阻塞,因为显然,URL中有字符保留为正则表达式,需要转义。
Boost库中是否有任何函数或方法为了逃避这种用法的字符串?我知道在大多数其他正则表达式中都有这样的方法,但是我在Boost中看不到。
或者,是否需要列出所有需要的字符被转载?
。 ^ $ | ()[] {} * +?讽刺的是,您可以使用正则表达式来转义您的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);
(标志指定使用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);
这篇关于如何转义用于Boost Regex的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!