• 正则表达式的方法


    #_author:Star
    #date:2019/11/8
    #正则表达式的方法
    #1. findall()-->所有的结果都返回到一个列表里
    #2. search()--->返回匹配到的第一个对象,对象可以调用group()方法返回结果
    #3. match()--->只在字符串开始匹配,,对象可以调用group()方法返回结果
    import re
    ret=re.match('star','gejhstar')
    print(ret)# None

    ret1=re.match('star','stargejh')
    print(ret1)# <re.Match object; span=(0, 4), match='star'>
    print(ret1.group()) # star
    #4.split
    ret2=re.split('m','asmagd')
    print(ret2)#['as','agd']
    ret3=re.split('[m,d]','asmagdoku') # ********
    print(ret3)# ['as', 'ag', 'oku']
    ret4=re.split('[m,d]','madgdoku') # ********
    print(ret4)# ['', 'a', 'g', 'oku'] 有 空是因为第一个m前面没有字母
    #5.sub()
    ret5=re.sub('s..r','good','comestarup')# -------->important
    print(ret5)# comegoodup
    #6.compile() 编译
    mat0=re.findall('.com','idhdiwwhi.comjdj')
    print(mat0) # ['.com']
    obj=re.compile('.com')
    mat=obj.findall('idhdiwwhi.comjdj')
    print(mat)# ['.com']
  • 相关阅读:
    php的四种算法
    laravel框架安装过程中遇到的问题
    json_decode转码无效
    php通过mysqli链接mysql数据库
    jq函数绑定与解绑
    redis的运行机制
    数据库设计的三范式
    MYSQL数据库索引
    PHP超全局变量数组
    vue的settings.json格式化配置
  • 原文地址:https://www.cnblogs.com/startl/p/11818933.html
Copyright © 2020-2023  润新知