Search code examples
pythonsplitnumbersarabicarabic-support

Split string and numbers with dashes in python


I have two lists chapter and verse

The text that I'm trying to add to these lists are as follows:

"الشورى22-23", "السجدة44-55"

translated into "Alshoura22-23", "Alsadjah44-55"

The ouput should be:

chapter = ["الشورى", "السجدة"]

verse = ["22-23", "44-55"]

I tried many methods such as regression and iterate through each characters but to no avail! Could you please help?

Thank you


Solution

  • Regular expressions provide a way to accomplish this goal.

    data = ["الشورى22-23", "السجدة44-55"]
    verse = [re.findall(r'\d+\-\d+', s)[0] for s in data]
    # ['22-23', '44-55']
    chapter = [data[i].replace(verse[i], '') for i in range(len(verse))]
    # ['الشورى', 'السجدة']