Reputation: 13
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
Upvotes: 1
Views: 204
Reputation: 163217
If you want to print both ways, you can use a pattern with capture groups to match the allowed characters, and then add the right group to the right collection:
For the current example strings:
import re
lst = ["الشورى22-23", "السجدة44-55"]
pattern = r"([\u0600-\u06FF]+)(\d+-\d+)"
verse = []
chapter = []
for s in lst:
m = re.match(pattern, s)
if m:
verse.append(m.group(1))
chapter.append(m.group(2))
print(chapter)
print(verse)
Output
['22-23', '44-55']
['الشورى', 'السجدة']
For both combinations, check the capture group values:
import re
lst = ["الشورى22-23", "السجدة44-55", "11-22السجدة"]
pattern = r"([\u0600-\u06FF]+)(\d+-\d+)|(\d+-\d+)([\u0600-\u06FF]+)"
chapter = []
verse = []
for s in lst:
for t in re.findall(pattern, s):
chapter.append(t[1] if t[1] else t[2])
verse.append(t[0] if t[0] else t[3])
print(chapter)
print(verse)
output
['22-23', '44-55', '11-22']
['الشورى', 'السجدة', 'السجدة']
Upvotes: 0
Reputation: 36496
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))]
# ['الشورى', 'السجدة']
Upvotes: 1