2

I want to write a regex expression for multiline comment in Python. I was trying to modify this expression for multiline comment in Java, but I wasn't able to do it, because in Python multiline comment works in a different way.

Regex expression for Java:

(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)

An example of multiline comment in Python:

''' comment comment
comment comment
'''

or

""" comment comment
comment comment
"""
2
  • You seem to confuse multiline string literals with multiline comments. See a regex for multiline Python comment here. Commented Mar 31, 2016 at 19:19
  • 1
    Thanks for the reply. But it seems that this link is offering a solution for a regular expression in Python for PHP multiline comment. Commented Mar 31, 2016 at 20:07

2 Answers 2

2

(Technically, multiline strings != multiline comments. But that's aside from the point)

The regex (['"])\1\1(.*?)\1{3} should work, but make sure you use re.DOTALL.

  • (['"]) Find a ' or " and capture it in \1
  • \1\1 Find 2 more of the same quotes
  • (.*?) Capture everything until...
  • \1{3} Find three more identical quotes
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply! Does this expression allows break lines? Because I was testing it in this website: link and it only recognized if the quotation marks were in the same line.
Did you see this: but make sure you use re.DOTALL? I put it there for a reason. Used like this: re.compile("regex", re.DOTALL)
0

the below work fine to catch the multi line comment block in python

\"""(.|[\r\n])*\"""

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.