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