Skip to content Skip to sidebar Skip to footer

Python 3 Regular Expression To Find Multiline Comment

I'm trying to find comment blocks in PHP source code using regular expressions in Python 3. The PHP comments are in this format: /** * This is a very short block comment */ Now

Solution 1:

You can use the re.DOTALL flag to make the . character match newlines:

re.compile(r'/\*\*.+?\*/', re.DOTALL)

(As a side note, PHP block comments can start with /*, not just /**.)

Solution 2:

Try this:

r'\/\*\*[^*]*\*+([^/][^*]*\*+)*\/'

(this is the regex used by some CSS parsers for /* CSS comments */, so I believe it is pretty solid)

It won't match the exact format including line breaks and the inner asterisks, but you can work around it. This will match:

/**
 * This is a very short block comment
 */

But also:

/** This is a very short block comment */

And even:

/** This is a very short block comment 
*/

To match the exact format of docblocks, you'd need a real parser, not regular expressions.

Post a Comment for "Python 3 Regular Expression To Find Multiline Comment"