Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multi-line match according to specific rules

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading