Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 1.08 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Removing C/C   style comments using boost::regex
  2. s#/*[^*]**+([^/*][^*]**+)*/|//([^\]|[^n][n]?)*?n|("(\.|[^"\])*"|'(\.|[^'\])*'|.[^/"'\]*)#defined $3 ? $3 : ""#gse;
  3.        
  4. std::string input = "hello /* world */ world";
  5.  
  6. boost::regex reg("(/\*([^*]|(\*+[^*/]))*\*+/)|(//.*)");
  7.  
  8. input = boost::regex_replace(input, reg, "");
  9.        
  10. std::string strip_comments(std::string const& input) {
  11.     std::string output;
  12.     typedef boost::wave::cpplexer::lex_token<> token_type;
  13.     typedef boost::wave::cpplexer::lex_iterator<token_type> lexer_type;
  14.     typedef token_type::position_type position_type;
  15.  
  16.     position_type pos;
  17.  
  18.     lexer_type it = lexer_type(input.begin(), input.end(), pos,
  19.         boost::wave::language_support(
  20.             boost::wave::support_cpp|boost::wave::support_option_long_long));
  21.     lexer_type end = lexer_type();
  22.  
  23.     for (;it != end; ++it) {
  24.         if (*it != boost::wave::T_CCOMMENT
  25.          && *it != boost::wave::T_CPPCOMMENT) {
  26.             output += std::string(it->get_value().begin(), it->get_value().end());
  27.         }
  28.     }
  29.     return output;
  30. }
  31.        
  32. *
  33.        
  34. \*
  35.        
  36. [^\]
  37.        
  38. [^\\]