group by line break and execute regex

Question:

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.

Asked By: 5th

||

Answers:

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.

Answered By: Wiktor Stribiżew
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.