如何使用正则表达式按多个分隔符分隔字符串,并有限制



我对拆分有一个非常不寻常的要求。我需要使用类似var1, var2 = string.split(delimiter, limit)的限制选项。但是,string.split不支持多个分隔符,这是我需要它做的。因此,我需要使用re.split。我如何限制re.splite或以其他方式实现此拆分?

理想情况下,我将能够输入类似的东西

contents = "Sentence? Sentence! Another sentence."

两个输出变量为:

var1 = "Sentence"
var2 = "Sentence! Another sentence."
>>> re.split('[?!.]', "Sentence? Sentence! Another sentence.", maxsplit=1)
['Sentence', ' Sentence! Another sentence.']

最新更新