1

I am trying to write a RegEx to parse groups of single line comments.

Given this input:

//line 1
//line 2
//line 3

//line 4
//line 5
//line 6

I expect to have two matches: Lines 1-3 and 4-6. With my current RegEx (^\/\/[\S\s]+$) I have one match: Lines 1-6, although there is an empty line in between.

The problem is that \s matches any whitespace character, so the blank line is included. But at the same time, line break are part of the RegEx – but only when the line starts with //, so I am stuck.

How can I prevent the RegEx from matching the blank line?

10
  • @RomanPerekhrest Because I want to learn it Commented Sep 23, 2017 at 19:12
  • If this can't be done with RegExes, then it's also knowlege :) I am just curious because initially I thought it should be possible Commented Sep 23, 2017 at 19:15
  • ok, good luck ... Commented Sep 23, 2017 at 19:22
  • 1
    Not an expert but this seems to work ^\/\/[\S\s]+?\s$ Commented Sep 23, 2017 at 19:24
  • @Brahma Dev: I like your solution even more! Commented Sep 23, 2017 at 19:32

2 Answers 2

2

This is what modifiers are for:

(?:^\/{2}.+\n?)+

With MULTILINE mode, see a demo on regex101.com.


Broken apart, this says:

(?:       # a non-capturing group
    ^     # start of the line
    \/{2} # //
    .+    # anything else in that line
    \n?   # a newline, eventually but greedy
 )+       # repeat the group
Sign up to request clarification or add additional context in comments.

Comments

2

You could try this one:

/(^\/\/[^\n]+$\n)+/gm

see here https://regex101.com/r/CrR9WU/1

This selects first the two / at the beginning of each line then anything that is not a newline and finally (at the end of the line) the newline character itself. There are two matches: rows 1 to 3 and rows 4 to 6. If you also allow "empty comment lines like // then this will do too:

/(^\/\/[^\n]*$\n)+/gm

Edit:
I know, it is a little late now, but Casimir's helpful comment got me on to this modified solution:

/(?:^\/\/.*\n?)+/gm

It solves the problem of the final \n, does not capture groups and is simpler. (And it is pretty similar to Jan's solution ;-) ...)

2 Comments

You can make more simple if you put the anchor ^ out the group, you don't need the $ anchor. . (the dot) matches all except \n like [^\n]. Also your pattern will fail if a line of comment is at the end of the string without a newline after it or if a comment line doesn't have characters after the slashes: /^\/\/.*(?:\n\/\/.*)*/gm
@Casimir: Thanks for pointing that out! Good alternativa regexp!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.