首页 > 代码库 > str.split()与re.split()的区别
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‘]
str.split()与re.split()的区别
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。