码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-02-08 18:45:18      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:括号   split   question   www   pre   tps   https   highlight   内容   

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‘]

 使用实例:

>>> url = "https://www.zhihu.com/question/34963917/answer/139938429"
>>> re.split("(https?://[\w.:]*)",url)
[‘‘, ‘https://www.zhihu.com‘, ‘/question/34963917/answer/139938429‘]

 

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

标签:括号   split   question   www   pre   tps   https   highlight   内容   

原文地址:http://www.cnblogs.com/zhming26/p/6379176.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!