re模块,hashlib模块

对着背影说爱祢 提交于 2020-02-10 16:59:47
import reprint(re.findall('alex','hahahah alex is alex is dsb'))                                               alexprint(re.findall('\w','Aah123 +-_'))  匹配字母数字下划线                              \wprint(re.findall('\w\w','Aah123 +-_'))                               \w\w['Aa','h1','23',]print(re.findall('\w9\w','Aa9h123 aaa9c+-_'))                                  \w9\w['a9h','a9c']print(re.findall('\W','Aah123 +-_'))  匹配非字母数字下划线print(re.findall('\s','Aah\t12\n3 +-_'))  匹配任意空白字符print(re.findall('\S','Aah\t12\n3 +-_'))  匹配任意非空字符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('\s','Aah\t12\n3 +-_'))  只匹配\sprint(re.findall('\t','Aah\t12\n3 +-_'))  只匹配\tprint(re.findall('\n','Aah\t12\n3 +-_'))  只匹配\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\nc',re.DOTALL))                                     a.c['a1c','aac','a c','a\nc'][]:代表匹配一个字符,这一个字符是来自于我们自定义的范围print(re.findall('a[0-9]c','a,c a a1c a9c aaac a c asfdsaf a\nc',re.DOTALL))print(re.findall('a[a-zA-Z]c','a,c aAc a1c a9c aaac a c asfdsaf a\nc',re.DOTALL))print(re.findall('a[a-zA-Z]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))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[^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 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','a\c aac'))print(re.findall(r'a\\c','a\c aac'))print(re.findall('alex','my name is alex Alex is dsb aLex ALeX',re.I))  忽略大小写    re.Iprint(re.findall('alex','my name is alex Alex is dsb aLex ALeX',re.I))msg="""my name is egonasdfsadfadfsadf egon123123123123123egon"""re.M :以\n为分隔符 为一行内容print(re.findall('egon$',msg,re.M)) #my name is egon\nasdfsadfadfsadf egon\n123123123123123egon'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 hashlibm=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()) #65c83c71cb3b2e2882f99358430679c3print(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())
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!