正則就是用一些具備特殊含義的符號組合到一塊兒(稱爲正則表達式)來描述字符或者字符串的方法。或者說:正則就是用來描述一類事物的規則.(在Python中)它內嵌在Python中,並經過 re 模塊實現。正則表達式
模式 | 舉例 | 解釋 | 結果 |
---|---|---|---|
\w | print ( re.findall ('\w','ab 12+- *&_') ) | 匹配字母數字下劃線 | ['a', 'b', '1', '2', '_'] |
\W | print ( re.findall ('\W','ab 12+- *&_') ) | 匹配非字母數字下劃線 | [' ', '\', '+', '-', ' ', '*', '&'] |
\s | print(re.findall('\s','ab \r1\n2\t+- *&_')) | 匹配任意空白字符 [\r\n\t\f] | [' ', '\r', '\n', '\t', ' '] |
\S | print(re.findall('\S','ab \r1\n2\t+- *&_')) | 匹配任意非空字符 | ['a', 'b', '1', '2', '\', '+', '-', '*', '&', '_'] |
\d | print(re.findall('\d','ab \r1\n2\t+- *&_')) | 匹配任意數字 [0-9] | ['1', '2'] |
\D | print(re.findall('\D','ab \r1\n2\t+- *&_')) | 匹配任意非數字 | ['a', 'b', ' ', '\r', '\n', '\t', '\', '+', '-', ' ', '*', '&', '] |
\A | print(re.findall('\Aalex','abcalex is sb')) | 匹配字符串的開始 | [ ] |
^ | print(re.findall('^alex','alex is salexb')) | 至關於\A | ['alex'] |
\Z | print(re.findall('sb\Z','alex is alexbsb')) | 匹配字符串的結尾 | ['sb'] |
$ | print(re.findall('sb$','alex is alexbsb')) | 至關於\Z | ['sb'] |
\n | print(re.findall('a\nc','a\nc a\tc a1c')) | 匹配一個換行符 | ['a\nc'] |
. | print(re.findall('a.c','abc a1c aaca\nc')) | 除了換行符外的任意一個字符 | ['abc', 'a1c', 'aac'] |
re.DOTALL | print(re.findall('a.c','abc a1c aaca\nc',re.DOTALL)) | 匹配包括換行符的任意一個字符 | |
? | print(re.findall('ab?','a ab abb abbb abbbb abbbb')) | 左邊一個字符重複0次或1次 | ['a', 'ab', 'ab', 'ab', 'ab', 'ab'] |
* | print(re.findall('ab*','a ab abb abbb abbbb abbbb a1bbbbbbb')) | 左邊一個字符出現0次或無窮次 | ['a', 'ab', 'abb', 'abbb', 'abbbb', 'abbbb', 'a'] |
+ | print(re.findall('ab+','a ab abb abbb abbbb abbbb a1bbbbbbb')) | 左邊一個字符出現1次或無窮次 | ['ab', 'abb', 'abbb', 'abbbb', 'abbbb'] |
{m,n} | print(re.findall('ab{0,1}','a ab abb abbb abbbb abbbb')) | 左邊一個字符出現m次到n次 | ['a', 'ab', 'ab', 'ab', 'ab', 'ab'] |
.* | print(re.findall('a.*c','ac a123c aaaac a *123)()c asdfasfdsadf')) | 匹配任意長度,任意的字符 貪婪匹配 | ['ac a123c aaaac a *123)()c'] |
.*? | print(re.findall('a.*?c','a123c456c')) | 非貪婪匹配 | ['a123c'] |
() | print(re.findall('(alex)_sb','alex_sb asdfsafdafdaalex_sb')) | 匹配括號內的表達式 | ['alex', 'alex'] |
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']
hash是一種算法,該算法接受傳入的內容,通過運算獲得一串hash值網絡
hash值的特色是:ui
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值相同spa
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
密碼加鹽3d
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() code
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())