小册名称:Python合辑4-130个字符串操作示例
在单词处拆分字符串并且或不保留分隔符
import restring = "Canada AND Japan NOT Audi OR BMW"l = re.split(r'(AND|OR|NOT)', string)print(l)
import re
string = "Canada AND Japan NOT Audi OR BMW"
l = re.split(r'(AND|OR|NOT)', string)
print(l)
Output:
['Canada ', 'AND', ' Japan ', 'NOT', ' Audi ', 'OR', ' BMW']