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

"x.rsplit()" with multiple delimiters in Python

I have this code:

x.rsplit("+", 1)[-1]

The string will be splitted at the end once if "+" is in the way.

Example:

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

12+345+32 –> 32

But I want it to split if "+", "-" or "/" are in the way (Not just with "+" but also with "-" or "/")

Example:

12+345 32 –> 32

or

12+345 / 32 –> 32

How can I add multiple limits when splitting?

>Solution :

You can use a regex to split the string based on the operators and return the rightmost result:

re.split(r"[+-/]", x)[-1]
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