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

Capture line breaks in subgroups

I thought I had an easy situation but somehow I can not figure out the right RegEx to do so.

Let us say I have text seperated on 3 different new lines like:

ABC
DEF
GHI

And I want to capture each line in a seperate group, but the regex should be able to match 1 ore more new lines.

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

Possible expected outcome from the RegEx is:

$0 : ABC
     BCD
     GHI
$1 : ABC
$2 : BCD
$3 : GHI

or in case theres only ABC with no follow up new lines

$0 : ABC
$1 : ABC

or in case theres 5 new lines

$0 : ABC
     BCD
     GHI
     JKL
     MNO
$1 : ABC
$2 : BCD
$3 : GHI
$4 : JKL
$5 : MNO

Would someone be able to produce a RegEx that would solve this question?

>Solution :

() to group
A-Z mayus chars

  • inside () to group all chars together
    \n or $ to mach ENTER or End of string

/ "g" to match all and "m" multiline

/([A-Z]+)(\n|$)/gm 
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