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

How to split a string in Python until one of specific characters occurs from right to left?

What is the best way to split a string into two parts in Python from right to left until one of several characters occur?

The aim is to separate a string into two parts with a version number (beginning with either A, B or C) at the end for examples like these:

  • EP3293036A1 -> EP3293036 + A1
  • US10661612B2 -> US10661612 + B2
  • CN107962948A -> CN107962948 + A
  • ES15258411C1 -> ES15258411 + C1

My code works for splitting the string for a single character:

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

first_part = number.rpartition('A')[0]
second_part = number.rpartition('A')[1] + number.rpartition('A')[2]

Is there a way to have multiple arguments like (‘A’ or ‘B’ or ‘C’) using rpartition? Or is there a better way using regex?

>Solution :

Try this.

import re

def split_re(s):
    return re.split(r'.(?=[ABC])+',s) # Change `ABC` to `A-Za-z` if you want a partition if any alphabetic character is present likt('A','a','z','Y')
print(split_re('EP3293036A1'))  # -> ['EP3293036', 'A1']
print(split_re('US10661612B2')) # -> ['US10661612', 'B2']
print(split_re('CN107962948A')) # -> ['CN107962948','A']
print(split_re('ES15258411C1')) # -> ['ES15258411', 'C1']
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