python cookbook学习笔记 -- DAY04

根据多个字符来分割字符串

知识点:re.split()
代码片段

'''
根据多个指定字符来分割字符串
知识点:re.split()
'''
def splitStringByMultCon():
    line = 'asdf fjdk; afed, fjek,asdf, foo'
    res = re.split(r'[;,\s]\s*',line) # 正则表达式 []表示匹配;,还有空格这三种,并且后面跟着任意空格也可以匹配
    print(res) # ['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo']
    res = re.split(r'(;|,|\s)\s*',line) # 使用括号捕获分组,被匹配的文本最终也会出现在结果列表中
    print(res) # ['asdf', ' ', 'fjdk', ';', 'afed', ',', 'fjek', ',', 'asdf', ',', 'foo']
    res = re.split(r'(?:;|,|\s)\s*',line) # 括号内使用?:的方式来说明,不需要匹配的文本出现在结果列表中去
    print(res) # ['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo']

字符串开头或者结果匹配

知识点:startwith()函数 和 endwith()函数
可以单字符串匹配,也可以通过传入包含多个字符串的元组进行多字符串匹配。list和set 需要先转为tuple后才可以传入
代码片段

'''
字符串开头或者结果匹配
知识点:startwith函数 和 endwith函数
可以单字符串匹配,也可以通过传入包含多个字符串的元组进行多字符串匹配
list和set 需要先转为tuple后才可以传入
'''
def startWidthAndEndWith():
    bookName = '程序员的自我修养'
    print(bookName.startswith('程序猿')) # False
    print(bookName.endswith('自我修养')) # True
    print(bookName.startswith(('程序员','程序员'))) # True 

使用shell通配符的方式来匹配文本

知识点:fnmatch函数和fnmatchcase函数 其中fnmatchcase函数是严格区分大小写的
代码片段

'''
使用shell通配符的方式来匹配文本
知识点:fnmatch函数和fnmatchcase函数 其中fnmatchcase函数是严格区分大小写的
'''
def matchWithShell():
    print(fnmatch('test.txt','*.txt')) # 任意字符匹配
    print(fnmatch('test.txt','?st.txt')) # 单字符匹配
    print(fnmatch('test.txt','??st.txt')) # 单字符匹配
    print(fnmatch('test.txt','?est.txt'))
    print(fnmatch('Dat451.csv','Dat[0-9]*.csv'))

对特定格式的字符串进行校验,一般选择正则表达式的方式来匹配

如果你想精确匹配,确保你的正则表达式以$ 结尾
如果你打算做大量的匹配和搜索操作的话,最好先编译正则表达式,
然后再重复使用它。模块级别的函数会将最近编译过的模式缓存起来,因此并
不会消耗太多的性能,但是如果使用预编译模式的话,你将会减少查找和一些额外的处理损耗
代码片段

def complexMatchByRe():
    text1 = '10/26/2022'
    text2 = 'DEC 26 2022'
    print(re.match(r'\d+/\d+/\d+',text1))
    if re.match(r'\d+/\d+/\d+',text1):
        print(True) # True 
    if re.match(r'\d+/\d+/\d+',text2):
        print(True)
    
    datepat = re.compile(r'\d+/\d+/\d+') # 一般将模式字符创预编译为模式对象,然后在对目标字符串进行匹配处理
    if datepat.match(text1):
        print(True) # True 
    if datepat.match(text2):
        print(True)

    text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
    res = datepat.findall(text)
    print(res) # ['11/27/2012', '3/13/2013']
    datepat = re.compile(r'(\d+)/(\d+)/(\d+)') # 使用括号的方式捕获分组可以让正则表达式更容易读懂
    
    m = datepat.match('10/26/2022')
    print(m) # <re.Match object; span=(0, 10), match='10/26/2022'>
    print(m.group(0),m.group(1),m.group(2),m.group(3),m.groups()) # 10/26/2022 10 26 2022 ('10', '26', '2022')
    # print(m.group(4)) # IndexError: no such group

    text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
    res = datepat.findall(text)
    print(res) # [('11', '27', '2012'), ('3', '13', '2013')]

    for month,day,year in datepat.findall(text): # findall() 方法会搜索文本并以列表形式返回所有的匹配
        print('{}-{}-{}'.format(year,month,day)) #2012-11-27 2013-3-13

    print(datepat.finditer(text)) # <callable_iterator object at 0x037B7AB0>
    for m in datepat.finditer(text): # 如果你想以迭代的方式返回匹配,可以使用finditer()方法来代替
        print(m.groups())

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值