8劈分

"""劈分"""


"""
在字符串中劈分子串时,使用模块re并通过正则表达式指定被劈分的子串可以实现更加强大的劈分功能。模块re提供了两个实现字符串劈分的方法
split(pattern, string[, maxsplit[, flags]])
"""

import re
# ['a', 'b', 'c', 'd', 'e', 'f']
print(re.split(r's+', 'a b c d e f')) # s表示任意空白
# ['a', 'b', 'c', 'd', 'e', 'f', '']
print(re.split(r's+', 'a b c d e f '))

pattern = re.compile(r'[s\,;]+')
# ['a', 'b', 'c', 'd', 'e', 'f']
print(pattern.split('a,b,,c ;,d e;;f'))
原文地址:https://www.cnblogs.com/sruzzg/p/12990159.html