• 正则表达式补充


    # 2.正则


    # 方法:findall | match | split | sub
    # a = 10
    # print(a.__hash__())
    # def fn():
    # pass
    # print(fn.__name__)
    # import json
    # print(json.dumps([1,2,3]))
    import re


    # 全文匹配,返回值是列表
    res = re.findall('d*?', 'd1') # ['', '', ''] | ['', '', '1', '']
    print(res)


    # 非贪婪匹配的引用场景: 一般都要结合特定的开头与结尾
    res = re.findall('<.*>', '<a>abc</a>')
    print(res) # ['<a>abc</a>']
    res = re.findall('<.*?>', '<a>abc</a>')
    print(res) # ['<a>', '</a>']
    res = re.findall('w*?>', '<a>abc</a>')
    print(res) # ['a>', 'a>']

    # 分组:通过分组加(),拿到匹配到的结果中的指定信息
    res = re.findall('((w*?)>)', '<a>abc</a>')
    print(res) # [('a>', 'a'), ('a>', 'a')]

    # 操作分组的方法
    # (?P<name>...): 有名分组
    # 返回值是match匹配的结果对象,可以.group(组序号|组名)来取具体组的信息
    res = re.match('(d{3})(?P<center>d{3})(d{3})', '123456789')
    print(res.group('center'))

    # 传入一个有正则语法的普通字符串,得到一个可以调用正则方法的正则字符串
    r = re.compile('(w*?)>')
    print(r.findall('<a>abc</a>'))

    # res = re.findall('<([a-z]{1,3})>(w*?)</[a-z]{1,3}>', '<a>abc</a><b>123</b>')
    # print(res) # [('a', 'abc'), ('b', '123')]

    res = re.sub('(d{3})(?P<center>d{3})(d{3})', r'213', '<123456789>')
    print(res)

    #

    [0-9]  #匹配数字0~9
    [a-z]  #匹配小写字母
    [A-Z]  #匹配大写字母
    d | D #匹配数字 | 非数字
    w| W       #匹配字母数字下划线 | 非字母数字下划线
    s | S      #匹配不可见字符: 空格等 | 可见字符
    :边界匹配, 按空格结束匹配  需要用r将正则转义,不然得不到想要的结果
    .

    a|b == [ab]
    [^msg]: msg的对立面

    *
    +
    ?

    *?
    +?
    ??
    '''
    import re
    print(re.findall(r'a[a-z]*', 'a ab c abc def ab'))
    print(re.findall(r'[^ab]', ' aab c abc def ab'))

    # 身份证:18位
    # 200000200808081111
    # [1-7][0-9]{5}
    # (?:19[0-9]{2}|20[01][0-9]): 1900 - 2019
    # (?:0[1-9]|1[0-2]): 01-12
    # (?:0[1-9]|[12][0-9]|3[01]) : 01-31
    # [0-9]{3}
    # [0-9Xx]
    # [1-7][0-9]{5}(?:19[0-9]{2}|20[01][0-9])(?:0[1-9]|1[0-2])(?:0[1-9]|[12][0-9]|3[01])[0-9]{3}[0-9Xx]

    # 邮箱
    # 30000000@qq.com
    # [a-zA-Z0-9]w{,15}@[176|178|192].[com|com.cn|cn]

    # 获取百度首页图片地址
    import requests
    responds = requests.get('https://www.baidu.com/')
    # print(responds.text)
    content = responds.text
    # www.baidu.com/img/gs.gif
    content += 'http://www.baidu.com/wimg/gs.gif'
    # res_list = re.findall('www[^w]*?(?:gif|png)', content)
    res_list = re.findall('www.{1,30}(?:gif|png)', content)
    # print(len(res_list))
    print(res_list)

  • 相关阅读:
    swift--使用URLSession异步加载图片
    swift--浮点数转换成整数(四舍五入/直接截断)
    swift--环形进度条(UIActivityIndicatorView)的用法
    swift--Timer实现定时器功能,每个一段时间执行具体函数,可以重复,也可以只执行一次
    HTML节点树
    网页的结构
    网页的组成
    HTTP 请求过程
    HTTP 基础术语
    《投资最重要的事》
  • 原文地址:https://www.cnblogs.com/maoruqiang/p/10723722.html
Copyright © 2020-2023  润新知