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.
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.
[;#%|*]
- Initial comment character.*
- Rest of first line(?:(?:\r\n|\r|\n) *[;#%|*].*)*
- Repeat 0 or more times:
(?:\r\n|\r|\n)
- Newline (if you know the format of your newline characters in advance, you can simplify this, eg, perhaps to just \n
)[;#%|*]
- Initial comment character.*
- Rest of line