
Untitled
By: a guest on
Apr 26th, 2012 | syntax:
None | size: 1.08 KB | hits: 13 | expires: Never
Removing C/C style comments using boost::regex
s#/*[^*]**+([^/*][^*]**+)*/|//([^\]|[^n][n]?)*?n|("(\.|[^"\])*"|'(\.|[^'\])*'|.[^/"'\]*)#defined $3 ? $3 : ""#gse;
std::string input = "hello /* world */ world";
boost::regex reg("(/\*([^*]|(\*+[^*/]))*\*+/)|(//.*)");
input = boost::regex_replace(input, reg, "");
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;
}
*
\*
[^\]
[^\\]