str.split()与re.split()的区别

时间:2022-05-07 02:57:30

str.split():

>>>'hello, world'.split()
>>>['hello,','world']
>>>'hello, world'.split(',')
>>>['hello',' world']

re.split():

re.split()方法可以使用正则表达式匹配,具体用法如下

>>>re.split(r'\W+','hello, world')>>>['hello','world']

如果使用带括号的正则表达式则可以将正则表达式匹配的内容也添加到列表内,例如

>>>re.split(r'(\W+)','hello, world')>>>['hello',', ','world']




本文出自 “sdu_IS” 博客,请务必保留此出处http://hychuanshuo.blog.51cto.com/2724628/1332474