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

  • Home
  • SEARCH
  • 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 198061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:54:22+00:00 2026-05-11T16:54:22+00:00

I’m parsing a source code file, and I want to remove all line comments

  • 0

I’m parsing a source code file, and I want to remove all line comments (i.e. starting with “//”) and multi-line comments (i.e. /…./). However, if the multi-line comment has at least one line-break in it (\n), I want the output to have exactly one line break instead.

For example, the code:

qwe /* 123
456 
789 */ asd

should turn exactly into:

qwe
asd

and not “qweasd” or:

qwe

asd

What would be the best way to do so?
Thanks


EDIT:
Example code for testing:

comments_test = "hello // comment\n"+\
                "line 2 /* a comment */\n"+\
                "line 3 /* a comment*/ /*comment*/\n"+\
                "line 4 /* a comment\n"+\
                "continuation of a comment*/ line 5\n"+\
                "/* comment */line 6\n"+\
                "line 7 /*********\n"+\
                "********************\n"+\
                "**************/\n"+\
                "line ?? /*********\n"+\
                "********************\n"+\
                "********************\n"+\
                "********************\n"+\
                "********************\n"+\
                "**************/\n"+\
                "line ??"

Expected results:

hello 
line 2 
line 3  
line 4
line 5
line 6
line 7
line ??
line ??
  • 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-11T16:54:22+00:00Added an answer on May 11, 2026 at 4:54 pm
    comment_re = re.compile(
        r'(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
        re.DOTALL | re.MULTILINE
    )
    
    def comment_replacer(match):
        start,mid,end = match.group(1,2,3)
        if mid is None:
            # single line comment
            return ''
        elif start is not None or end is not None:
            # multi line comment at start or end of a line
            return ''
        elif '\n' in mid:
            # multi line comment with line break
            return '\n'
        else:
            # multi line comment without line break
            return ' '
    
    def remove_comments(text):
        return comment_re.sub(comment_replacer, text)
    
    • (^)? will match if the comment starts at the beginning of a line, as long as the MULTILINE-flag is used.
    • [^\S\n] will match any whitespace character except newline. We don’t want to match line breaks if the comment starts on it’s own line.
    • /\*(.*?)\*/ will match a multi-line comment and capture the content. Lazy matching, so we don’t match two or more comments. DOTALL-flag makes . match newlines.
    • //[^\n] will match a single-line comment. Can’t use . because of the DOTALL-flag.
    • ($)? will match if the comment stops at the end of a line, as long as the MULTILINE-flag is used.

    Examples:

    >>> s = ("qwe /* 123\n"
             "456\n"
             "789 */ asd /* 123 */ zxc\n"
             "rty // fgh\n")
    >>> print '"' + '"\n"'.join(
    ...     remove_comments(s).splitlines()
    ... ) + '"'
    "qwe"
    "asd zxc"
    "rty"
    >>> comments_test = ("hello // comment\n"
    ...                  "line 2 /* a comment */\n"
    ...                  "line 3 /* a comment*/ /*comment*/\n"
    ...                  "line 4 /* a comment\n"
    ...                  "continuation of a comment*/ line 5\n"
    ...                  "/* comment */line 6\n"
    ...                  "line 7 /*********\n"
    ...                  "********************\n"
    ...                  "**************/\n"
    ...                  "line ?? /*********\n"
    ...                  "********************\n"
    ...                  "********************\n"
    ...                  "********************\n"
    ...                  "********************\n"
    ...                  "**************/\n")
    >>> print '"' + '"\n"'.join(
    ...     remove_comments(comments_test).splitlines()
    ... ) + '"'
    "hello"
    "line 2"
    "line 3 "
    "line 4"
    "line 5"
    "line 6"
    "line 7"
    "line ??"
    "line ??"
    

    Edits:

    • Updated to new specification.
    • Added another example.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 206k
  • Answers 206k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer foo.__module__ should return bar If you need more info, you… May 12, 2026 at 9:04 pm
  • Editorial Team
    Editorial Team added an answer I would query into an anonymous type and then map… May 12, 2026 at 9:04 pm
  • Editorial Team
    Editorial Team added an answer That's the official list : http://www.nvidia.com/object/cuda_learn_products.html Your 9500GS isn't listed… May 12, 2026 at 9:04 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.