If i have a list of prefix that can be attached to a string, how do i split a string such into it’s prefix and the other characters in the next substring. For example:
prefixes = ['over','under','re','un','co']
str1 = "overachieve"
output: ["over","achieve"]
str2 = "reundo"
output = ["re","un","do"]
Is there a better way to do the above task, maybe with regex or some string functions other than:
str1 = "reundo"
output = []
for x in [p for p in prefixes if p in str1]:
output.append(x)
str1 = str1.replace(x,"",1)
output.append(str1)
Regular expressions are an efficient way to search for many alternative prefixes: