str.split()與re.split()的區別


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM