移除C/C++;使用boost::regex设置注释样式 我试图用正则表达式从字符串中删除C和C++风格的注释。我找到了一个Perl,它似乎可以同时做到这两个方面:
移除C/C++;使用boost::regex设置注释样式 我试图用正则表达式从字符串中删除C和C++风格的注释。我找到了一个Perl,它似乎可以同时做到这两个方面:,c++,regex,boost,boost-regex,C++,Regex,Boost,Boost Regex,s\\/\*[^*]*\*+([^/*][^*]*\*+)*/\\//([^\].[^\n][\n]?)*?\n |(“(\.\.\\.[^\])*(\\.[^'\\])*“\\..[^/'\]*”)*”。[^/'\]*)\35;定义为3美元$3.:#gse 但我不确定如何将其用于boost::regex代码块,或者我需要做什么才能将其转换为boost::regex所接受的正则表达式 仅供参考:我在这里找到了正则表达式:它似乎涵盖了我需要的任何情况 我不希望使用boost::spirit::qi
s\\/\*[^*]*\*+([^/*][^*]*\*+)*/\\//([^\].[^\n][\n]?)*?\n |(“(\.\.\\.[^\])*(\\.[^'\\])*“\\..[^/'\]*”)*”。[^/'\]*)\35;定义为3美元$3.:#gse代码>
但我不确定如何将其用于boost::regex
代码块,或者我需要做什么才能将其转换为boost::regex
所接受的正则表达式
仅供参考:我在这里找到了正则表达式:它似乎涵盖了我需要的任何情况
我不希望使用boost::spirit::qi
来完成这项工作,因为这将为项目的编译增加大量时间
编辑:
std::string input = "hello /* world */ world";
boost::regex reg("(/\\*([^*]|(\\*+[^*/]))*\\*+/)|(//.*)");
input = boost::regex_replace(input, reg, "");
因此,较短的正则表达式确实有效,但较长的正则表达式无效。
\*
变成
\\*
那为什么不呢
[^\\]
变成
[^\\\\]
如果
变成
\\*
那为什么不呢
[^\\]
变成
[^\\\\]
似乎有点奇怪,当Boost已经有C++预处理器库()时,可以使用正则表达式来处理注释。
std::string strip_comments(std::string const& input) {
std::string output;
typedef boost::wave::cpplexer::lex_token<> token_type;
typedef boost::wave::cpplexer::lex_iterator<token_type> lexer_type;
typedef token_type::position_type position_type;
position_type pos;
lexer_type it = lexer_type(input.begin(), input.end(), pos,
boost::wave::language_support(
boost::wave::support_cpp|boost::wave::support_option_long_long));
lexer_type end = lexer_type();
for (;it != end; ++it) {
if (*it != boost::wave::T_CCOMMENT
&& *it != boost::wave::T_CPPCOMMENT) {
output += std::string(it->get_value().begin(), it->get_value().end());
}
}
return output;
}
std::string strip\u注释(std::string const&input){
std::字符串输出;
typedef boost::wave::cpplexer::lex_-token_-type;
typedef boost::wave::cpplexer::lex_迭代器lexer_type;
typedef token_type::position_type position_type;
位置\ U型位置;
lexer\u type it=lexer\u type(input.begin(),input.end(),pos,
boost::wave::语言支持(
boost::wave::support_cpp | boost::wave::support_option_long_long);
lexer_type end=lexer_type();
for(;it!=end;++it){
如果(*it!=boost::wave::T\u cMont
&&*it!=boost::wave::T_(注释){
output+=std::string(it->get_value().begin(),it->get_value().end());
}
}
返回输出;
}
当Boost已经有一个C++预处理器库()可以用来删除注释时,你会使用正则表达式,这有点奇怪。
std::string strip_comments(std::string const& input) {
std::string output;
typedef boost::wave::cpplexer::lex_token<> token_type;
typedef boost::wave::cpplexer::lex_iterator<token_type> lexer_type;
typedef token_type::position_type position_type;
position_type pos;
lexer_type it = lexer_type(input.begin(), input.end(), pos,
boost::wave::language_support(
boost::wave::support_cpp|boost::wave::support_option_long_long));
lexer_type end = lexer_type();
for (;it != end; ++it) {
if (*it != boost::wave::T_CCOMMENT
&& *it != boost::wave::T_CPPCOMMENT) {
output += std::string(it->get_value().begin(), it->get_value().end());
}
}
return output;
}
std::string strip\u注释(std::string const&input){
std::字符串输出;
typedef boost::wave::cpplexer::lex_-token_-type;
typedef boost::wave::cpplexer::lex_迭代器lexer_type;
typedef token_type::position_type position_type;
位置\ U型位置;
lexer\u type it=lexer\u type(input.begin(),input.end(),pos,
boost::wave::语言支持(
boost::wave::support_cpp | boost::wave::support_option_long_long);
lexer_type end=lexer_type();
for(;it!=end;++it){
如果(*it!=boost::wave::T\u cMont
&&*it!=boost::wave::T_(注释){
output+=std::string(it->get_value().begin(),it->get_value().end());
}
}
返回输出;
}
使用两个单独的正则表达式不是更容易理解和维护吗?首先,摆脱/*…*/那就扔掉/。。。下线。@jmucchiello不能;它们相互影响。还有,你到底为什么要这样做?你为什么要用C++来做?2 plz(注意向下投票,编程点:(.这太奇怪了)你为什么不使用预处理器来做这件事呢?gcc-E
,等等。@acidzombie24:当然他们可以交互。你可以用/*
注释掉一个/*/
,或者用/
注释掉一个//
。使用两个单独的正则表达式不是更容易理解和维护它吗?首先,去掉/*…*/然后去掉//…eol。@jmuCcCHILO不能,它们互相影响。另外,为什么你会这样做?为什么你在C++中做?- 2 PLZ(注:下注,编程点:(这太奇怪)你为什么不使用预处理器来做这件事呢?gcc-E
,等等。@acidzombie24:当然他们可以交互。你可以用/*
注释掉/*/
或*/
。