split适用于单个分隔符
当需要多个分隔时,可以逐次使用split()
re.split(pattern, string, maxsplit=0) #原型
re.split()的使用有两种方式:
re.split("[ !]", text) #[]里边放的是需要的分隔符,中间没有空格和逗号
re.split(",|!", text) #中间用 | 隔开
当是多个单一分隔符时,适合用第一种,第二种需要考虑某些字符需要转义使用
re.split("[;,?]")
re.split(";|,|\?", text)
当多个长短不一的分隔符时,适合第二种
re.split(r";|,|\?\s|;\s|,\s", text)
当需要多个分隔时,可以逐次使用split()
text = "abc def!" sg = text.split() sg[1] = sg[1].split('!')[0] print(sg) #['abc', 'def']这种情况下,使用re.split()较为方便
re.split(pattern, string, maxsplit=0) #原型
re.split()的使用有两种方式:
re.split("[ !]", text) #[]里边放的是需要的分隔符,中间没有空格和逗号
re.split(",|!", text) #中间用 | 隔开
当是多个单一分隔符时,适合用第一种,第二种需要考虑某些字符需要转义使用
re.split("[;,?]")
re.split(";|,|\?", text)
当多个长短不一的分隔符时,适合第二种
re.split(r";|,|\?\s|;\s|,\s", text)
text = "abc,l def!" re.split("[, !]", text) #['abc', 'l', 'def',''] re.split(",| |!", text) #['abc', 'l', 'def','']两个的结果最终是一样的