1

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
"""
halfer
  • 19,824
  • 17
  • 99
  • 186
Marcos Guimaraes
  • 1,243
  • 4
  • 27
  • 50
  • You seem to confuse multiline string literals with multiline comments. See a [regex for multiline Python comment here](http://stackoverflow.com/questions/7081882/python-3-regular-expression-to-find-multiline-comment). – Wiktor Stribiżew Mar 31 '16 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. – Marcos Guimaraes Mar 31 '16 at 20:07

2 Answers2

1

(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
Laurel
  • 5,965
  • 14
  • 31
  • 57
  • Thanks for the reply! Does this expression allows break lines? Because I was testing it in this website: [link](https://regex101.com/r/wY1hU9/1#python) and it only recognized if the quotation marks were in the same line. – Marcos Guimaraes Mar 31 '16 at 20:03
  • 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)` – Laurel Mar 31 '16 at 20:20
0

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

\"""(.|[\r\n])*\"""
issam
  • 93
  • 3