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

Break long regex into multiple lines

I have this long regex in the code bellow

import re

message = "/sell 2000 USDT @ 5.56 111.222.333-44 user@example.com"

regex = re.compile(r"^/(?P<operation>\w+) (?P<amount>.+) (?P<network>.+) @ (?P<rate>.+) (?P<legal>.+) (?P<contact>.+)")

result = regex.match(message)

print(result.groupdict())

I want know if it is possible to break each group in one line, to make more readable. Unfortunately my input data is on a single line, is it possible?

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 :

Just split it up with quotes

regex = re.compile(r"^/(?P<operation>\w+)"
                   " (?P<amount>.+)"
                   " (?P<network>.+)"
                   " @ (?P<rate>.+)"
                   " (?P<legal>.+)"
                   " (?P<contact>.+)")

{'operation': 'sell', 'amount': '2000', 'network': 'USDT', 'rate': '5.56', 'legal': '111.222.333-44', 'contact': 'user@example.com'}
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