正则表达式解决方案(对我来说)似乎很容易:
import re
def split_string(source,separators):
return re.split('[{0}]'.format(re.escape(separators)),source)
例子:
>>> import re
>>> def split_string(source,separators):
... return re.split('[{0}]'.format(re.escape(separators)),source)
...
>>> split_string("the;foo: went to the store",':;')
['the', 'foo', ' went to the store']
在这里使用正则表达式的原因是如果您不想在' '
分隔符中使用,这仍然有效......
另一种(我认为我更喜欢)可以使用多字符分隔符的方法是:
def split_string(source,separators):
return re.split('|'.join(re.escape(x) for x in separators),source)
在这种情况下,多字符分隔符作为某种非字符串可迭代(例如元组或列表)传入,但单字符分隔符仍然可以作为单个字符串传入。
>>> def split_string(source,separators):
... return re.split('|'.join(re.escape(x) for x in separators),source)
...
>>> split_string("the;foo: went to the store",':;')
['the', 'foo', ' went to the store']
>>> split_string("the;foo: went to the store",['foo','st'])
['the;', ': went to the ', 'ore']
或者,最后,如果您还想在连续运行的分隔符上进行拆分:
def split_string(source,separators):
return re.split('(?:'+'|'.join(re.escape(x) for x in separators)+')+',source)
这使:
>>> split_string("Before the rain ... there was lightning and thunder.", " .")
['Before', 'the', 'rain', 'there', 'was', 'lightning', 'and', 'thunder', '']