拆分 split

40.9 拆分 split

re.split(模式, 文本) 按正则切分,比 str.split 更灵活(如多种分隔符)。

按空格/逗号/分号切分

# ========================================
# 示例:re.split
# 说明:[\s,;]+ 表示连续空白或逗号或分号
# ========================================
import re

s = 'apple, banana;  cherry   date'
parts = re.split(r'[\s,;]+', s)
print(parts)  # ['apple','banana','cherry','date']