2

Here is the code I've tried,

from re import *


commentStart = compile('/\*')
commentEnd = compile('\*/')
singleComment = compile('//')
quotes = compile('".*"')

def readComment(line):

    while(line):
        if(commentEnd.match(line)):
            return
        line = input()

line=input()

while(line):
    if(quotes.match(line)):
        print(line)
        line = input()
        continue

    elif(commentStart.match(line)):

        readComment(line)
        line=input()
        continue

    elif(singleComment.match(line)):
        line=input()
        continue

    else:
        print(line)

    line=input()

I'm able to remove the single line comments but i have problem with the multi line comments.

Sample input:

abcd
//blah
efg
/*blah
blah
blah*/
hij

My Output:

abcd
efg

Expected Output:

abcd
efg
hij

Please point out where I've made mistake. Thankyou.

1
  • 1
    On SO, if you've received an answer you can use you should mark it accepted. Commented Feb 20, 2017 at 17:19

1 Answer 1

4

This one:

commentEnd.match(line)

Should be:

commentEnd.search(line)

From the docs:

If you want to locate a match anywhere in string, use search() instead

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.