Bearing in mind the proverb “two problems”, I would still say that this is work for regular expression. Regular commands are compiled into final machines, which check all possible options in parallel, and not one by one.
Here's an implementation that uses the following:
import re def split_string(string, prefixes): regex = re.compile('|'.join(map(re.escape, prefixes))) # (1) while True: match = regex.match(string) if not match: break end = match.end() yield string[:end] string = string[end:] if string: yield string # (2) prefixes = ['over','under','re','un','co'] assert (list(split_string('recouncoundo',prefixes)) == ['re','co','un','co','un','do'])
Notice how the regular expression in (1) is built:
- prefixes are escaped using
re.escape , so special characters do not interfere - escaped prefixes are combined using the
| (or) regex - all information is compiled.
Line (2) gives the final word if any of those remaining after the separation prefixes. You may need to remove the if string check if string you want the function to return an empty string if there is nothing left after removing the prefix.
Also note that re.match (unlike re.search ) only searches for the pattern at the beginning of the input line, so there is no need to add ^ to the regular expression.
source share