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

splitting a string with multiple delimiters doesn't work

I’m trying to split a string suing multiple delimiters. Somehow it just doesn’t work, it only works if I use each delimiter separately. What am I doing wrong?

string = '\r\n\r\n \r\n\r\nfirst_col: col1 \r\n\r\nsecond_col: col2 \r\n\r\nthird_col: col3 \r\n\r\nfourth_col: col4 \r\n\r\nfith_col: col5\r\n'
splitter = "\r\n\r\n | \r\n\r\n| "
string.split(splitter)

>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

Use re.split() to split the string according to a regex pattern:

import re

string = '\r\n\r\n \r\n\r\nfirst_col: col1 \r\n\r\nsecond_col: col2 \r\n\r\nthird_col: col3 \r\n\r\nfourth_col: col4 \r\n\r\nfith_col: col5\r\n'
splitter = "\r\n\r\n | \r\n\r\n| "

new_string = re.split(splitter, string)
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