-3

How do I match any line containing #Test and also all next lines from it as long they start with:

  • any amount of space followed by a ;

Example:

 1.        ; #Test
 2.        ; bbbb
 3.    
 4.        ; #Test
 5.
 6.        ; aaa

Line 1 and 2 are one match, line 4 another match

This is what i got atm: \s*(#Test).*(\s*;.*)*

https://regex101.com/r/HvPAxt/1

My current doubt is how to stop matching when an empty line is found.

Example2:

 1.        ; #Test
 2.        ; bbbb
 3. 
 4. xxxx
 5. ; #Test
 6. yyyy
 7.
 8.    ; #Test
 9.
 10.   ; bbb

Line 1 and 2 are one match

Line 5 another match

Line 8 is another match.

Cesar
  • 41
  • 2
  • 5
  • 16

1 Answers1

2

The problem with your RE is that \s also matches newlines.

You need to use a regular expression that is explicit about newlines, since you have specific requirements about them.

So I would use [ \t]* to match spaces and tabs, instead of \s*:

[ \t]*(#Test).*(\n[ \t]*;.*)*

PS: Make sure you don't use the s option (single line) because then . will start matching the newline character too.

joanis
  • 10,635
  • 14
  • 30
  • 40
  • Your regex is not matching when its just a single line, ex: `; #Test` – Cesar Jul 15 '22 at 16:38
  • I thought the next line had to start with ;, if I misread your question let me revise. – joanis Jul 15 '22 at 16:38
  • OK, just reread your question more carefully, you want all subsequent lines that start with ; to be included. I'll figure it out and edit my answer. – joanis Jul 15 '22 at 16:40
  • Fixed. I was just missing a `*` after the last block, so accept any number of subsequent lines that start with ; – joanis Jul 15 '22 at 16:41