Chrysippus
Chrysippus

Reputation: 119

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.

Upvotes: 1

Views: 139

Answers (2)

CertainPerformance
CertainPerformance

Reputation: 370729

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)
    • space followed by * - 0 or more spaces
    • [;#%|*] - Initial comment character
    • .* - Rest of line

Upvotes: 1

Emma
Emma

Reputation: 27723

My guess is that here we might want an expression that'd pass newlines, such as

[;#%|*]([\s\S].*?)(?=[\r\n])

DEMO

Upvotes: 0

Related Questions