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

Split a string with multiple delimiter & create a separate list

I have a python string that contains a list of words separated by either a plus or minus sign

s = "AA + BB + 1C - CC - DD"

I want to get a list of words with a plus sign which is below

plusList = ["AA", "BB", "1C"]

And a list with a minus sign as below

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

minusList = ["CC", "DD"]

Any help appreciated.
Thank you!

>Solution :

import re

s = "AA + BB + 1C - CC - DD"

plusList = re.findall(r"(?:^|\+\s*)(\w+)", s)
minusList = re.findall(r"(?:\-\s*)(\w+)", s)

print(plusList)
print(minusList)
<script src="https://cdn.jsdelivr.net/gh/pysnippet/pysnippet@latest/snippet.min.js"></script>
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