zoukankan      html  css  js  c++  java
  • re模块,hashlib模块

    import re

    print(re.findall('alex','hahahah alex is alex is dsb'))
    alex

    print(re.findall('w','Aah123 +-_'))  匹配字母数字下划线
    w
    print(re.findall('ww','Aah123 +-_'))
    ww
    ['Aa','h1','23',]

    print(re.findall('w9w','Aa9h123 aaa9c+-_'))
    w9w
    ['a9h','a9c']

    print(re.findall('W','Aah123 +-_'))  匹配非字母数字下划线
    print(re.findall('s','Aah 12 3 +-_'))  匹配任意空白字符
    print(re.findall('S','Aah 12 3 +-_'))  匹配任意非空字符
    print(re.findall('d','Aah 12 3 +-_'))  匹配任意数字
    print(re.findall('D','Aah 12 3 +-_'))  匹配任意非数字


    print(re.findall('wwdd','asfdasdfegon001adfadfegon002asdfxx01 yy02'))

    print(re.findall('s','Aah 12 3 +-_'))  只匹配\s
    print(re.findall(' ','Aah 12 3 +-_'))  只匹配\t
    print(re.findall(' ','Aah 12 3 +-_'))  只匹配\n


    ^: 仅从头开始匹配
    print(re.findall('^alex',' alex is alex is alex'))
    ^alex

    $: 仅从尾部开始匹配

    print(re.findall('alex$',' alex is alex is alex1'))
    alex$


    .: 代表匹配一个字符,该字符可以是除换行符之外任意字符
    print(re.findall('a.c','a a1c aaac a c asfdsaf a c',re.DOTALL))
    a.c
    ['a1c','aac','a c','a c']

    []:代表匹配一个字符,这一个字符是来自于我们自定义的范围
    print(re.findall('a[0-9]c','a,c a a1c a9c aaac a c asfdsaf a c',re.DOTALL))
    print(re.findall('a[a-zA-Z]c','a,c aAc a1c a9c aaac a c asfdsaf a c',re.DOTALL))
    print(re.findall('a[a-zA-Z]c','a,c aAc a1c a9c aaac a c asfdsaf a c',re.DOTALL))
    print(re.findall('a[+*/-]c','a,c a+c a-c a*c a/c aAc a1c a9c aaac a c asfdsaf a c',re.DOTALL))
    print(re.findall('a[+*-/]c','a,c a+c a-c a*c a/c aAc a1c a9c aaac a c asfdsaf a c',re.DOTALL))

    print(re.findall('a[^0-9]c','a,c a a1c a9c aaac a c asfdsaf a c',re.DOTALL))



    重复匹配
    ?:代表左边那一个字符出现0次到1次
    print(re.findall('ab?','a ab abb abbbb a123b a123bbbb'))
    ab?
    ['a','ab','ab','ab','a','a']

    *: 代表左边那一个字符出现0次到无穷次
    print(re.findall('ab*','a ab abb abbbb a123b a123bbbb'))
    ab*
    ['a','ab','abb','abbbb','a','a']

    +: 代表左边那一个字符出现1次到无穷次
    print(re.findall('ab+','a ab abb abbbb a123b a123bbbb'))
    ab+
    ['ab','abb','abbbb']

    {n,m}:代表左边那一个字符出现n次到m次
    print(re.findall('ab{1,3}','a ab abb abbbb a123b a123bbbb'))
    ['ab', 'abb', 'abbb']
    print(re.findall('ab{1,}','a ab abb abbbb a123b a123bbbb'))
    print(re.findall('ab+','a ab abb abbbb a123b a123bbbb'))

    print(re.findall('ab{0,}','a ab abb abbbb a123b a123bbbb'))
    print(re.findall('ab*','a ab abb abbbb a123b a123bbbb'))

    print(re.findall('ab{3}','a ab abb abbbb a123b a123bbbb'))


    .*: 匹配任意0个到无穷个字符,贪婪匹配
    print(re.findall('a.*c','a123213123asdfasdfc123123123123+-0)((c123123'))
    a.*c

    .*?:匹配任意0个到无穷个字符,非贪婪匹配
    print(re.findall('a.*?c','a123213123asdfasdfc123123123123+-0)((c123123'))


    |:或者
    print(re.findall('companies|company','Too many companies have gone bankrupt,c and the next one is my company'))
    companies|company

    ():分组  默认只取组内内容
    print(re.findall('compan(?:ies|y)','Too many companies have gone bankrupt,c and the next one is my company'))
    compan(ies|y)

    print(re.findall('href="(.*?)"','<p>动感视频</p><a href="https://www.douniwan.com/1.mp4">逗你玩呢</a><a href="https://www.xxx.com/2.mp4">葫芦娃</a>'))
    href=".*?"

    ?:取消组内
    'a\c'
    print(re.findall('a\\c','ac aac'))
    print(re.findall(r'a\c','ac aac'))


    print(re.findall('alex','my name is alex Alex is dsb aLex ALeX',re.I))  


    忽略大小写    re.I
    print(re.findall('alex','my name is alex Alex is dsb aLex ALeX',re.I))

    msg="""
    my name is egon
    asdfsadfadfsadf egon
    123123123123123egon
    """
    re.M :以\n为分隔符 为一行内容
    print(re.findall('egon$',msg,re.M)) #my name is egon asdfsadfadfsadf egon 123123123123123egon'

    re模块其他方法
    res=re.findall('(href)="(.*?)"','<p>动感视频</p><a href="https://www.douniwan.com/1.mp4">逗你玩呢</a><a href="https://www.xxx.com/2.mp4">葫芦娃</a>')
    print(res)

    re.search:只匹配成功的一次,没有则返回None,匹配第一个内容,以及内容的索引位置
    res=re.search('(href)="(.*?)"','<p>动感视频</p><a href="https://www.douniwan.com/1.mp4">逗你玩呢</a><a href="https://www.xxx.com/2.mp4">葫芦娃</a>')
    print(res)
    print(res.group(0))   默认只取第一个分组
    print(res.group(1))  只取第一个分组
    print(res.group(2))  只取第二个分组


    res=re.match('abc','123abc') ## res=re.search('^abc','123abc')  默认从头开始找,找到就结束.
    print(res)

    print(re.findall('alex','alex is alex is alex'))
    print(re.search('alex','alex is alex is alex'))
    print(re.match('alex','alex is alex is alex'))

    pattern=re.compile('alex')    comile:公用表达式
    print(pattern.findall('alex is alex is alex'))
    print(pattern.search('alex is alex is alex'))
    print(pattern.match('alex is alex is alex'))


    ['1', '2', '60', '-40.35', '5', '-4', '3']
    msg="1-2*(60+(-40.35/5)-(-40*3))"
    print(re.findall('D?(-?d+.?d*)',msg))

    msg="1-2*(60+(-40.35/5)-(-40*3))"
    D?-?d+.?d*




    hashlib模块


    '''
    1. 什么是hash
    hash是一种算法,该算法接受一系列的数据,经过运算会得到一个hash值,
    hash值具备三大特性:
    1. 只要传入的内容一样,那么得到的hash值一定是一样
    2. 只要采用hash算法固定,无论传入的内容多大,hash值的长度是固定

    3. hash值不可逆,即不能通过hash值逆推出内容

    2. 为何要用hash

    特性1+2=>文件完整性校验
    特性3==>加密

    '''
    特性1:
    import hashlib

    m=hashlib.md5()
    m.update('你好'.encode('utf-8'))
    m.update('hello'.encode('utf-8'))
    print(m.hexdigest()) #65c83c71cb3b2e2882f99358430679c3

    特性2:
    m1=hashlib.md5()
    m1.update('你好hello'.encode('utf-8'))
    print(m1.hexdigest()) #65c83c71cb3b2e2882f99358430679c3
    print(len(m1.hexdigest())) #32

    特性3:
    m2=hashlib.sha512()
    m2.update(b'asdfassssssssssssssssssssssssssss')
    print(m2.hexdigest())
    print(len(m2.hexdigest()))




    with open(r'D:脱产5期内容day17今日内容',mode='rb') as f:
    m=hashlib.md5()
    for line in f:
    m.update(line)
    print(m.hexdigest())



    pwd=input('password>>> ').strip()
    m=hashlib.md5()
    m.update('天王盖地虎'.encode('utf-8'))
    m.update(pwd.encode('utf-8'))
    m.update('一行白鹭上青天'.encode('utf-8'))
    print(m.hexdigest())





  • 相关阅读:
    源码分析— java读写锁ReentrantReadWriteLock
    7. SOFAJRaft源码分析—如何实现一个轻量级的对象池?
    深入理解Kafka必知必会(2)
    6. SOFAJRaft源码分析— 透过RheaKV看线性一致性读
    5. SOFAJRaft源码分析— RheaKV中如何存放数据?
    深入理解Kafka必知必会(1)
    4. SOFAJRaft源码分析— RheaKV初始化做了什么?
    3. SOFAJRaft源码分析— 是如何进行选举的?
    2. SOFAJRaft源码分析—JRaft的定时任务调度器是怎么做的?
    pinpoint1.8.5安装及使用指南
  • 原文地址:https://www.cnblogs.com/huangchaonan/p/10078536.html
Copyright © 2011-2022 走看看