代码点读机 / Python / re.split

re.split Python · 模块功能

按正则表达式分割字符串。

怎么用

import re
re.split(pattern, s)

小例子

import re
print(re.split(r'[,;]', 'a,b;c'))

运行结果

['a', 'b', 'c']

提醒:比 str.split 更灵活。

官方文档:https://docs.python.org/zh-cn/3/library/re.html#re.split

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条