6.16 재 모듈
특별한 의미 나 함께 방법을 설명 문자열이 일부 기호 조합의 정기적 인 사용은 (같은 정규 표현식 함). 또는 : 일반 규칙 (파이썬에서) 일이 파이썬에 내장하고, 다시 모듈에 의해 구현되는 클래스를 설명하는 데 사용됩니다.
모드 | 예를 들면 | 설명 | 결과 |
---|---|---|---|
\ w | 인쇄 (re.findall ( '\ w', 'AB 12 + - _ *')) | 영숫자와 밑줄을 일치 | [ 'A', 'B', '1', '2', '_'] |
\ W | 인쇄 (re.findall ( '\의 W', 'AB 12 + - _ *')) | 영숫자가 아닌 문자와 밑줄을 일치 | [ '', ' \ ', '+', '-', ','* ','& '] |
\에스 | 인쇄 (re.findall ( '\의 S', 'AB \ R1 \ N2 \ t + - _ *')) | 공백 [\ 연구 \ n \ T는 \의 F]을 일치 | [ ','\ R ','\ n을 ','\의 t '' '] |
\에스 | 인쇄 (re.findall ( '\의 S', 'AB \ R1 \ N2 \ t + - _ *')) | 공백이 아닌 문자와 일치 | [ 'A', 'B', '1', '2', ' \ ', '+', '-', '*', '&', '_'] |
\디 | 인쇄 (re.findall ( '\의 D', 'AB \ R1 \ N2 \ t + - _ *')) | 개수 [0-9] 일치 | [ '1', '2'] |
\디 | 인쇄 (re.findall ( '\의 D', 'AB \ R1 \ N2 \ t + - _ *')) | 비 숫자 일치 | [ 'A', 'B', ','\ R ','\ n을 ','\의 t '를,' \ ','+ ','- ',', '*', '&', '] |
\에이 | 인쇄 (re.findall ( '\ Aalex', 'abcalex는 SB입니다')) | 문자열의 시작 부분과 일치 | [] |
^ | 인쇄 (re.findall ( '^ 알렉스') '알렉스 salexb입니다') | 를 \ 동등 | [알렉스 '] |
\ Z | 인쇄 (re.findall ( 'SB의 \의 Z') '알렉스 alexbsb입니다') | 문자열의 끝 | [ 'SB'] |
$ | 인쇄 (re.findall ( 'SB의 $') '알렉스 alexbsb입니다') | \ Z 상당 | [ 'SB'] |
\엔 | 인쇄 (re.findall ( '는 \의 NC', '는 \ 고 급 \ TC A1C')) | 줄 바꿈을 일치 | [ '는 \의 NC'] |
. | 인쇄 (re.findall ( 'A.C', 'ABC A1C AACA \ NC')) | 어떤 개행 문자에 추가 | [ 'ABC', '당화', 'AAC'] |
re.DOTALL | 인쇄 (re.findall ( 'A.C', 'ABC의 A1C AACA \ NC', re.DOTALL)) | 모든 문자를 포함하여 줄 바꿈을 일치 | |
? | 인쇄 (re.findall ( 'AB?', '이 AB ABB abbb abbbb abbbb')) | 왼쪽 한 문자 반복 0 또는 1 | [ 'A'는 '두', '인'인 '인'인 '] |
* | 인쇄 (re.findall ( 'AB *', '는 ABB AB abbb abbbb abbbb a1bbbbbbb')) | 왼쪽 문자가 0 또는 무한대 번 표시 | [ 'A', 'B', 'ER', 'abbb', 'abbbb', 'abbbb', 'A'] |
+ | 인쇄 (re.findall ( 'AB +', '는 ABB AB abbb abbbb abbbb a1bbbbbbb')) | 문자를 왼쪽 한 번 또는 무한대 시간 이상 표시 | [ '현재', '아베'Abbb 'Abbbb'Abbbb '] |
{M, N} | 인쇄 (re.findall ( 'AB {0,1}' '은 AB ABB abbb abbbb abbbb')) | M 시간 문자는 왼쪽 n 번에 나타납니다 | [ 'A'는 '두', '인'인 '인'인 '] |
. * | 인쇄 (re.findall ( '이. * C', '교류 a123c의 aaaac의 A * 123) () asdfasfdsadf C')) | 어떤 길이의 일치는 욕심이 모든 문자와 일치 | [ '교류 a123c의 aaaac의 A * 123) () (C')] |
. *? | 인쇄 (re.findall ( '는. *? C', 'a123c456c')) | 비 욕심 경기 | [ 'a123c'] |
() | 인쇄 (re.findall ( '(알렉스) _sb', 'alex_sb asdfsafdafdaalex_sb')) | 괄호 안에 식을 일치 | [알렉스 ','알렉스 '] |
print(re.findall('^ebn$','ebn1')) #[]
print(re.findall('href="(.*?)"','<li><a id="blog_nav_sitehome" class="menu"href="http://www.cnblogs.com/">博客园</a></li>')
) #['http://www.cnblogs.com/']
print(re.findall('a[0-9][0-9]c','a1c a+c a2c a9c a11c a-c acc aAc'))#[]:匹配一个指定范围内的字符(这一个字符来自于括号内定义的) #['a11c']
print(re.findall('a[-+*]c','a1c a+c a2c a9c a*c a11c a-c acc aAc')) #当-需要被当普通符号匹配时,只能放到[]的最左边或最右边 ['a+c', 'a*c', 'a-c']
print(re.findall('a[a-zA-Z]c','a1c a+c a2c a9c a*c a11c a-c acc aAc')) #['acc', 'aAc']
print(re.findall('a[^a-zA-Z]c','a c a1c a+c a2c a9c a*c a11c a-c acc aAc'))# []内的^代表取反的意思
#['a c', 'a1c', 'a+c', 'a2c', 'a9c', 'a*c', 'a-c']
print(re.findall('([a-z]+)_sb','egon alex_sb123123wxxxxxxxxxxxxx_sb,lxx_sb'))#['alex', 'wxxxxxxxxxxxxx', 'lxx']
print(re.findall('compan(ies|y)','Too many companies have gone bankrupt, and the next one is my company')) #| :或者 ['ies', 'y']
print(re.findall('compan(?:ies|y)','Too many companies have gone bankrupt, and the next one is my company')) #(?:):代表取匹配成功的所有内容,而不仅仅只是括号内的内容 ['companies', 'company']
re模块的其他方法:
print(re.findall('alex|sb','123123 alex sb sadfsadfasdfegon alex sb egon'))#['alex', 'sb', 'alex', 'sb']
print(re.search('alex|sb','123213 alex sb sadfsadfasdfegon alex sb egon').group())#只到找到第一个匹配,然后返回一个包含匹配信息的对象 #alex
print(re.search('^alex','123213 alex sb sadfsadfasdfegon alex sb egon'))# None 匹配不成功返回None而不是[]
print(re.search('^alex','alex sb sadfsadfasdfegon alex sb egon').group()) #alex
print(re.match('alex','alex sb sadfsadfasdfegon alex sb egon').group()) #alex
print(re.match('alex','123213 alex sb sadfsadfasdfegon alex sb egon')) #None
#search+^可以代替match
info='a:b:c:d'
print(info.split(':')) #['a', 'b', 'c', 'd']
print(re.split(':',info)) #['a', 'b', 'c', 'd']
info=r'get :a.txt\3333/rwx'
print(re.split('[ :\\\/]',info)) #['get', '', 'a.txt', '3333', 'rwx']
print('egon is beutifull egon'.replace('egon','EGON',1)) #EGON is beutifull egon
#123 egon is beutifull EGON 123
print(re.sub('(.*?)(egon)(.*?)(egon)(.*?)',r'\1\2\3EGON\5','123 egon is beutifull egon 123'))
#(123)(egon)( is beutifull )(egon)(123)
print(re.sub('(lqz)(.*?)(SB)',r'\3\2\1',r'lqz is SB'))# SB is lqz
#(lqz)(is)(SB)
print(re.sub('([a-zA-Z]+)([^a-zA-Z]+)([a-zA-Z]+)([^a-zA-Z]+)([a-zA-Z]+)',r'\5\2\3\4\1',r'lqzzzz123+ is SB')) #SB123+ is lqzzzz #(lqzzzz)(123+ )(is)( )(SB)
pattern=re.compile('alex')
print(pattern.findall('alex is alex alex')) #['alex', 'alex', 'alex']
print(pattern.findall('alexasdfsadfsadfasdfasdfasfd is alex alex')) #['alex', 'alex', 'alex']
6.17 hashlib模块
hash是一种算法,该算法接受传入的内容,经过运算得到一串hash值
hash值的特点是:
1 只要传入的内容一样,得到的hash值必然一样(用于明文传输密码、文件完整性校验) 2 不能由hash值返解成内容=======》把密码做成hash值,不应该在网络传输明文密码 3 只要使用的hash算法不变,无论校验的内容有多大,得到的hash值长度是固定的
import hashlib
m=hashlib.md5()
m.update('hello'.encode('utf-8'))
m.update('world'.encode('utf-8'))
m.update('egon'.encode('utf-8'))
print(m.hexdigest()) #3801fab9b8c8d9fcb481017969843ed5
import hashlib
m=hashlib.md5()
m.update('h'.encode('utf-8'))
m.update('e'.encode('utf-8'))
m.update('lloworld'.encode('utf-8'))
m.update('egon'.encode('utf-8'))
print(m.hexdigest()) #3801fab9b8c8d9fcb481017969843ed5
注意:m.update()无论是多次传值还是一次传值,得到的hash值相同
import hashlib
m=hashlib.md5()
with open(r'C:\Users\Desktop\上节课复习','rb') as f:
for line in f:
m.update(line)
hv=m.hexdigest()
print(hv) #98416536bdf1f0dc0776629f501ae469
密码加盐
import hashlib
m=hashlib.md5()
pwd='alex3714'
m.update('天王盖地虎'.encode('utf-8'))
m.update(pwd.encode('utf-8'))
m.update('小鸡炖蘑菇'.encode('utf-8'))
print(m.hexdigest()) #ab44c43ea02e8c1083346ca707a6f572
hashlib.sha256(),hashlib.sha512()
import hashlib
m=hashlib.md5()
m.update('helloworld'.encode('utf-8'))
print(m.hexdigest()) #fc5e038d38a57032085441e7fe7010b0
m=hashlib.sha256()
m.update('helloworld'.encode('utf-8'))
print(m.hexdigest()) #936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af
m=hashlib.sha512()
m.update('helloworld'.encode('utf-8'))
print(m.hexdigest()) #1594244d52f2d8c12b142bb61f47bc2eaf503d6d9ca8480cae9fcf112f66e4967dc5e8fa98285e36db8af1b8ffa8b84cb15e0fbcf836c3deb803c13f37659a60
hmac 模块 ,它内部对我们创建 key 和 内容 进行进一步的处理然后再加密
import hmac
m=hmac.new('天王盖地虎,小鸡炖模块'.encode('utf-8'))
m.update('alex3814'.encode('utf-8'))
print(m.hexdigest())