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 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

Related Questions

I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.