I am trying to extract a block of text followed by one or more identifiers (separated by a slash) and having the identifiers be grouped with the block of text using regex in python.
Here is some dummy example data:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
EX 0-02a
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Vestibulum eget vestibulum sapien.
Sed porta, odio id sollicitudin congue
EX 0-04a
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Vestibulum eget vestibulum sapien, sed porta, odio id sollicitudin congue
Lorem ipsum dolor sit amet
EX 0-01a / EX 0-05a / EX 0-07a /
EX 0-08a
Lorem ipsum dolor sit amet,
consectetur adipiscing
elit.
Vestibulum eget
vestibulum sapien.
EX 3-11b
So far, I have created this regex:
(.*?)(\w{2,4} \d-\d{0,2}\w)
This successfully captures the text and one singular code, but some blocks have multiple codes (sometimes on multiple lines) and these are not grouped together as the /'s are being picked up as matches.
This is the kind of output I need:
[(Lorem ipsum dolor sit amet, consectetur adipiscing elit.), (EX 0-01a / EX 0-02a / EX 0-05b / EX 0-03a)]
Regex101: https://regex101.com/r/Wfc4nj/1