shanau2

导航

 

今日内容

1. re&正则表达式(*****)

注:不要将自定义文件命名为re
    
    import re
    re.findall(正则表达式,被匹配的字符串) 拿着正则表达式去字符串中找,返回一个列表
    
    re.findall('alex','hhhhh alex is alex is dsb') # 找出其中所有的alex
    
    以上只表示一种情况,可以用用特殊符号代替多种字符
    print(re.findall('\w','Aah123 +-')) # \w表示一次匹配一个字母数字或下划线 ,从头开始匹配完整个字符串
    print(re.findall('\w\w','Aah123 +-_')) # 一次匹配两个字符(字母数字下划线),一直匹配到结尾
    print(re.findall('\w9\w','Aa9h123 aaa9c+-_'))
    
    \W:匹配非数字字母下划线
    print(re.findall('\W','Aah123 +-_'))
    \s:匹配任意空白字符
    print(re.findall('\s','Aah\t12\n3 +-_'))
    \S:匹配任意非空字符
    print(re.findall('\S','Aah\t12\n3 +-_'))
    \d:匹配任意数字
    print(re.findall('\d','Aah\t12\n3 +-_'))
    \匹配任意非数字
    print(re.findall('\D','Aah\t12\n3 +-_'))
    
    # 按照两个字符数字下滑线+两个数字的正则表达式,去匹配后面的字符串
    print(re.findall('\w\w\d\d','asfdasdfegon001adfadfegon002asdfxx01 yy02'))
    
    print(re.findall('\t','Aah\t12\n3 +-_')) # 只在字符串中匹配制表符
    print(re.findall('\n','Aah\t12\n3 +-_')) # 只在字符串中匹配换行符
    
    ^:仅仅从头开始匹配,只匹配开头,匹配不到就不往后匹配了
    print(re.findall('^alex',' alex is alex is alex'))
    
    $:仅从尾部开始匹配,匹配不上就不往前匹配了
    
    .代表一个字符,该字符可以是除换行符之外的任意字符
    re.DOTALL 让.匹配所有字符包括换行符
    print(re.findall('a.c','a a1c aaac a c asfdsaf a\nc',re.DOTALL))
    
    # a开头,c结尾,中间可以是1-9直接任意的一个数字
    print(re.findall('a[0-9]c','a,c a a1c a9c aaac a c asfdsaf a\nc',re.DOTALL))
    #a开头c结尾,中间的一个字符可以是a-z或者A-Z之间的任意字符
    print(re.findall('a[a-zA-Z]c','a,c aAc a1c a9c aaac a c asfdsaf a\nc',re.DOTALL))
    
    #a开头c结尾,中间可以是+-*/任意一个字符,因为-处在中间时候,有范围的含义,所以放在最后避免歧义
    print(re.findall('a[+*/-]c','a,c a+c a-c a*c a/c aAc a1c a9c aaac a c asfdsaf a\nc',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\nc',re.DOTALL))
    
    # 在方框[]的范围前加上^,代表取反,a开头,c结尾,中间除了0-9的字符都可以
    print(re.findall('a[^0-9]c','a,c a a1c a9c aaac a c asfdsaf a\nc',re.DOTALL))
    
    []:代表匹配一个字符,但这一个字符可以是莲子与我们自定义的范围
    
    重复匹配
    ?:代表左边那一个字符出现0次或者1次
    
    print(re.findall('ab?','a ab abb abbbb a123b a123bbb'))

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

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

    {n,m}:代表左边那一个字符出现n次或者m次
    print(re.findall('ab{1,3}','a ab abb abbbb a123b a123bbb'))

    组合使用
    .*:匹配0个到无穷个字符,贪婪匹配,a开头,匹配到最后一个c结束,有害的
    print(re.findall('a.*c','a123213123asdfasdfc123123123123+-0)((c123123'))

    
    .*?:匹配任意0个到1次,找到最近的一个匹配字符,非贪婪匹配
    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'))
    
    ()匹配成功,只留下括号内匹配成功的部分,加上?:会把
    print(re.findall('compan(ies|y)','Too many companies have gone bankrupt,c and the next one is my company'))
    
    (?:a|b)匹配成功后会把整个匹配内容都返回
    print(re.findall('compan(?:ies|y)','Too many companies have gone bankrupt,c and the next one is my company'))
    
    # 匹配到最近的.就会结束
    print(re.findall('href="(.*?)"','<p>动感视频</p><a href="https://www.douniwan.com/1.mp4">逗你玩呢</a><a href="https://www.xxx.com/2.mp4">葫芦娃</a>'))
    
    
    a\\c,python语法会先转化成a\c,交给c语言的正则表达式功能
    print(re.findall('a\\\\c','a\c aac')) # 两个转义符号,转成两个斜杠形式,交给c语言的正则表达式功能
    print(re.findall(r'a\\c','a\c aac')) # 也可以在正则表达式前加r,表示原生字符串格式
    
    re.I 忽略匹配时候的大小写
    # print(re.findall('alex','my name is alex Alex is dsb aLex ALeX',re.I))

    re.M 以换行符\n分割字符串,再一行一行匹配
    msg = '''
    my name is egon
    sadasdasdad egon
    123124123123egon'''
    print(re.findall('egon$',msg,re.M))
    
    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 只匹配一次就结束
    print(res.group(0)) # 取出所有分组的内容
    print(res.group(1)) # 取出第一个分组的内容
     print(res.group(2)) # 取出第二个分组的内容

    可以控制()分开的组内元素
    
    re.match 是从头开始匹配的,开头找不到就不往后找了
    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')
    print(pattern.findall'alex is alex is alex')) 
    print(pattern.search('alex is alex is alex'))
    print(pattern.match('alex is alex is alex'))
    
    
    小练习:
    msg="1-2*(60+(-40.35/5)-(-40*3))",取出msg中所有的数字
    应该是['1', '2', '60', '-40.35', '5', '-4', '3']
    print(re.findall('\D?(-?\d+\.?\d*)',msg))

2. hashlib(*****)

1.什么是hash
        hash是一种算法,该算法接受一系列的数据,经过运算会得到一个hash值
        hash值具备三大特性
            1.只要传入的内容一样,那么得到的hash值一定是一样
            2.只要采用的hash算法固定,无论传入的内容多大,hash值得长度是固定的
            3.hash值不可逆,即不能通过hash值推出内
    2.为何要用hash
        特性1+2==>文件完整性校验
        特性3==>
    import hashlib
    m = hashlib.md5()
    m.update('hello'.encode('utf-8'))
    m.update('hello'.encode('utf-8'))
    print(m.hexdigest())
    
    m1=hashlib.md5()
    m1.update('你好hello'.encode('utf-8'))
    print(m1.hexdigest()) 
    print(len(m1.hexdigest())) #32
    
    
    m2=hashlib.sha512() # 不同的hash算法最后得到的值位数不一样
    m2.update(b'asdfassssssssssssssssssssssssssss')
    print(m2.hexdigest())
    print(len(m2.hexdigest()))
    
    md5 算法的hash值32位
    
    文件过大的时候,如果所有内容都读取的话,hash值会计算过慢
    with open(r'D:\脱产5期内容\day17\今日内容',mode='rb') as f:
        m=hashlib.md5()
        for line in f:
            m.update(line)
        print(m.hexdigest())
    所以可以在文件中节选多段位置,生成hash值,减少生成和校验hash值得时间
    
    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())
    # 个人密码中可以通过字符串加盐的方式,密码穿插暗号,生成密码,这样会降低撞库的风险
posted on 2018-12-06 16:16  shanau2  阅读(124)  评论(0编辑  收藏  举报