Python重新分割为“”或“”

时间:2018-11-12 20:37:48

标签: python regex

试图重新拆分以正常工作。 输入=“ a1 a2 a3,a4,a5”

expecting output = ['a1','a2','a3','a4','a5']
s = re.split(',|\s', "a1 a2 a3, a4,a5")
getting output = ['a1','a2','a3',' ','a4','a5']

1 个答案:

答案 0 :(得分:8)

您必须允许一个或多个分割字符:

>>> re.split('[,\s]+', "a1 a2 a3, a4,a5")
['a1', 'a2', 'a3', 'a4', 'a5']