6.16 re模块

正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法。或者说:正则就是用来描述一类事物的规则.(在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']

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())

python 之 re模块、hashlib模块的更多相关文章

  1. Python第十一天 异常处理 glob模块和shlex模块 打开外部程序和subprocess模块 subprocess类 Pipe管道 operator模块 sorted函数 os模块 hashlib模块 platform模块 csv模块

    Python第十一天    异常处理  glob模块和shlex模块    打开外部程序和subprocess模块  subprocess类  Pipe管道  operator模块   sorted函 ...

  2. Python操作数据库及hashlib模块

    一.hashlib模块 hashlib模块,主要用于加密相关的操作,在python3的版本里,代替了md5和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA51 ...

  3. Python进阶(九)----json模块, pickle模块, os模块,sys模块,hashlib模块

    Python进阶----json模块, pickle模块, os模块,sys模块,hashlib模块 一丶序列化模块 什么是序列化: ​ 将一种数据结构,转换成一个特殊的序列(特殊字符串,用于网络传输 ...

  4. Python 入门之 内置模块 -- hashlib模块

    Python 入门之 内置模块 -- hashlib模块 1.hashlib 摘要算法,加密算法 (1)主要用途: <1> 加密 : md5 sha1 sha256 sha512 md5, ...

  5. 小白的Python之路 day5 hashlib模块

    hashlib模块 一.概述 用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 二.算法的演 ...

  6. python全栈开发-hashlib模块(数据加密)、suprocess模块、xml模块

    一.hashlib模块 1.什么叫hash:hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 ...

  7. python模块: hashlib模块, configparse模块, logging模块,collections模块

    一. hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用 ...

  8. python模块——hashlib模块(简单文件摘要算法实现)

    #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = "loki" # Usage: hashlib模块 import ...

  9. python day 8: re模块补充,导入模块,hashlib模块,字符串格式化,模块知识拾遗,requests模块初识

    目录 python day 8 1. re模块补充 2. import模块导入 3. os模块 4. hashlib模块 5. 字符串格式:百分号法与format方法 6. 模块知识拾遗 7. req ...

  10. python笔记7 logging模块 hashlib模块 异常处理 datetime模块 shutil模块 xml模块(了解)

    logging模块 日志就是记录一些信息,方便查询或者辅助开发 记录文件,显示屏幕 低配日志, 只能写入文件或者屏幕输出 屏幕输出 import logging logging.debug('调试模式 ...

随机推荐

  1. NPAPI绘图和事件处理

    https://developer.mozilla.org/en-US/docs/Plugins/Guide/Drawing_and_Event_Handling 本章介绍如何确定插件实例是窗口化还是 ...

  2. Win10安装PyQt5与Qt Designer【转】

    https://blog.csdn.net/u011342224/article/details/78879633 1.直接在cmd中通过pip安装PyQt5 1 pip install pyqt5 ...

  3. [转]OpenGL编程指南(第9版)环境搭建--使用VS2017

    1.使用CMake Configure中选择VS2017 Win64 , Finish: 点击Generate. 2.进入build目录 打开GLFW.sln , 生成解决方案. 打开vermilio ...

  4. Eclipse创建的Java Web项目,如何启用外置浏览器访问jsp或者html页面

    当我们用Eclipse创建了一个Java Web项目,想访问一个jsp或者html页面时,通常会在目标页面(以jsp为例)上点击鼠标右键,选择[Run As]——>[Run on Server] ...

  5. 让remix使用本地文件系统

    让remix使用本地文件系统   转:https://blog.csdn.net/platocnet/article/details/83376792 1. 测试发现使用npm命令安装相关环境不成功, ...

  6. typeScript模块<三>

    /*模块 1 模块的的概念 2 模块导出的几种方法 1.export 导出声明 2.export 导出语句 3.export default 4.import导入模块 3 模块化封装上一讲的DB库 * ...

  7. 先查询再插入,改为存储过程,java部分入参出参、mybatisxml【我】

    先查询再插入,改为存储过程 create or replace procedure PRO_REVENUE_SI(l_p_cd in Varchar2, l_c_cd in Varchar2, l_p ...

  8. rf安装对应requests库的方法

    先要安装requests,再安装requestsLibrary pip install requests pip install robotframework-requests github地址 ht ...

  9. python中验证码连通域分割的方法详解

    python中验证码连通域分割的方法详解 这篇文章主要给大家介绍了关于python中验证码连通域分割的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需 ...

  10. Uncaught TypeError: TableInit is not a constructor

    我最近在做东西的时候,用到了Bootstrap的表格,我复制了一份代码使用,结果运行报错 Uncaught TypeError: TableInit is not a constructor 我点进去 ...