我一直在尝试使用正则表达式作为分隔符来拆分字符串,但 的输出string.split
似乎包含一些冗余结果。
import re;
replaceArray = '((Replace the string)|((in|inside|within) the string)|(with the string))'
stringToSplit = '(Replace the string arr1 in the array arr2 with the array arr3)'
print(re.split(replaceArray, stringToSplit))
我希望拆分字符串看起来像这样,没有任何重叠的结果:
['Replace the string', ' arr1 ', 'in the string', ' arr2 ', 'with the string', ' arr3']
但是,拆分字符串数组包含一些冗余结果,这些结果似乎与其他匹配的字符串重叠:
['', 'Replace the string', 'Replace the string', None, None, None, ' arr1 ', 'in the string', None, 'in the string', 'in', None, ' arr2 ', 'with the string', None, None, None, 'with the string', ' arr3']
有什么方法可以防止这些冗余和重叠的结果包含在输出中string.split
?