Home > Back-end >  group by line break and execute regex
group by line break and execute regex

Time:11-13

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.

CodePudding user response:

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.

  • Related