Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7072897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:51:52+00:00 2026-05-28T05:51:52+00:00

Assume that I have a var std::string sourceCode; where I have loaded a cpp

  • 0

Assume that I have a var std::string sourceCode; where I have loaded a cpp source file. Now I want to remove all comments with the included regex classes from tr1 (now they are fully included as I use the Microsoft compiler) – single-line is easy but multi-line not. It is not about just replacing a comment with a space etc. it’s about to keep the correct number of lines. Assume we remove a comment which is 5 lines long, this space should then be filled with 5 newlines so that I am able to backtrack code and compute with the correct line numbers.

My code so far:

std::regex singleLinedCommentReg("//.*");
sourceCode = std::regex_replace(sourceCode, singleLinedCommentReg, std::string(""));
std::regex multiLinedCommentReg("(/\\*([^*]|[\r\n]|(\\*+([^*/]|[\r\n])))*\\*+/)");
std::for_each(
    std::sregex_iterator(sourceCode.begin(), sourceCode.end(), multiLinedCommentReg),
    std::sregex_iterator(),
    [&](const std::match_results<std::string::const_iterator>& match) -> bool {
        // TODO: Replace the current match with an appropriate number of newlines.
        return true;
    }
);

Can anyone give me some advice on that?

EDIT #1

I do NOT want to provoke comments about the discussion whether it makes sense to use RegEx for this kind of doing! Please just assume the input is clean and as expected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:51:53+00:00Added an answer on May 28, 2026 at 5:51 am

    Your approach using regex is way off and too complicated. You are trying to use a regular language (regex) to parse a situation that is at least as complex as a context-free grammar. If you split things up and do part of the processing in C++ you’ll get it done but it’ll look messy.

    If your goal is to write a function that strips all of the comments out without losing the new line characters I suggest that you generate a parse using one of the many parsing tools available.

    This took less than 5 minutes to create and is functionally what you are looking for. You can modify this to your hearts content. It will generate a lexer with flex 2.5.4 or flex 2.5.35

    %{
        #include <stdio.h>
    %}
    
    
    cbeg    "/*"
    cend    "*/"
    cppc    "//"
    nl  "\n"|"\r\n"
    
    %option noyywrap
    %x mlc 
    %%
    {nl}        { fputs(yytext, stdout); }
    {cbeg}      { BEGIN(mlc); }
    {cend}      { fprintf(stderr, "Error: found end of comment without a beginning\n"); return -1; }
    {cppc}.*    /* eat up the comment */
    .       { fputs(yytext, stdout); }
    
    <mlc>{cend} { BEGIN(INITIAL); }
    <mlc>{cbeg}     { fprintf(stderr, "Error: Found /* inside another /* comment"); return -1; }
    <mlc>.      /* eat up everything else */
    
    %%
    
    int main(int argc, char* argv[])
    {
            yylex();
    }
    

    Addendum:

    The above is a fully functional program. You can generate the .c using:

    flex -t foo.l > foo.c
    

    and you can compile it using

    cc -o foo foo.c
    

    Now something like

    ./foo < source.c > source-sans-comments.c 
    

    will generate the new source file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume that we have N erlang nodes, running same application. I want to share
Assume that I have the ItemSource bound and i am getting the data. Now
(Let's assume that I have good reasons not to remove my NSAssert() checks in
assume that I have this list: <ul id=carousel> <li><a title=hey><img src=image.png/></a></li> </div> And now
Let's assume that we have simple jQuery code like the following: var $document =
Say I have the following code: var album = new MyObject('album'); Assume that when
Hi assume that I have a JSON like this: var myJson = [{id:111,name:aaa,surname:bbb}, {id:222,name:ccc,surname:ddd}]
Assume that you have a running SQL Server Express instance named (local)\SQLEXPRESS. Its database
Assume that I have this piece of code: @interface Foo : NSObject { Bar
Assume that we have multiple arrays of integers. You can consider each array as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.