regexplaintext

Match multiple line comment blocks composed of one or more single line comments


I need a regex that will match comment blocks composed of one or more single line comments.

Single Line Comment:

# This is a single line comment

Comment Block Composed of Multiple Single Line Comments:

# This is a multiple line comment
# which is just a block of single line comments
# that are strung together

The first character of a comment line can begin with any of the following characters: ;#%|*

I have found the following regex matches individual comment lines: [;#%|*]{1}(.+)

But I cannot figure out how to match for blocks that have more than one line. I want to keep all characters in the whole block, including new lines.


Solution

  • Match the start of a comment, the rest of its line, then repeat 0 or more occurences of a group which starts with a newline, optional spaces, followed by the a comment start character and the rest of the line:

    [;#%|*].*(?:(?:\r\n|\r|\n) *[;#%|*].*)*
    

    See this regex demo.