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

group by line break and execute regex

From one connector, I receive a very specific string format:

{#{123}#};{#{abc}#}\n{#{345}#};{#{def}#}\n{#{789}#};{#{ghi}#}

I can’t change that format. Currently, I use the regex (?s){#{(.*?)}#}. This results in a list ["123", "abc", "345", "def", "789", "ghi"]

Is it possible to get the output grouped by line? Something like [("123", "abc"), ("345", "def"), ("789", "ghi")] – I know i can also just use split in python. Is there any regex way? I tried a bit with www.regex101.com, however couldnt find a solution.

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

>Solution :

Since you have pairs of matches on each line, you can capture them:

re.findall(r'{#{(.*?)}#};{#{(.*?)}#}', text)

See the Python demo.

Output:

[('123', 'abc'), ('345', 'def'), ('789', 'ghi')]

Note you should not use (?s) DOTALL inline flag since it makes . match across lines.

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