python-re模块-hashlib模块
文章目录
- 一、re模块
- 二、hashlib模块
一、re模块
print(re.findall('alex','hahahah alex is alex is dsb')) #['alex', 'alex']#\w 匹配字母数字及下划线
print(re.findall('\w','Aah123 +-_')) #['A', 'a', 'h', '1', '2', '3', '_']
print(re.findall('\w\w','Aah123 +-_')) #['Aa', 'h1', '23']
print(re.findall('\w9\w','Aa9h123 aaa9c+-_')) #['a9h', 'a9c']#\W 匹配非字母数字下划线
print(re.findall('\W','Aah123 +-_')) #[' ', '+', '-']#\s 匹配任意空白字符,等价于[\t\n\r\f]
print(re.findall('\s','Aah\t12\n3 +-_')) #['\t', '\n', ' ']
#\S 匹配任意非空字符
print(re.findall('\S','Aah\t12\n3 +-_')) #['A', 'a', 'h', '1', '2', '3', '+', '-', '_']
#\d 匹配任意数字,等价于[0-9]
print(re.findall('\d','Aah\t12\n3 +-_')) #['1', '2', '3']
#\D 匹配任意非数字
print(re.findall('\D','Aah\t12\n3 +-_')) #['A', 'a', 'h', '\t', '\n', ' ', '+', '-', '_']print(re.findall('\w\w\d\d','asfdasdfegon001adfadfegon002asdfxx01 yy02')) #['on00', 'on00', 'xx01', 'yy02']#\t匹配一个制表符
print(re.findall('\t','Aah\t12\n3 +-_'))
#\n匹配一个换行符
print(re.findall('\n','Aah\t12\n3 +-_'))
# ^匹配字符串的开头
print(re.findall('^alex','alex is alex is alex')) #['alex']
# $匹配字符串的末尾
print(re.findall('alex$',' alex is alex is alex1')) #[]
# .代表匹配一个字符,该字符可以是除换行符之外任意字符
print(re.findall('a.c','a a1c aaac a c asfdsaf a\nc',re.DOTALL)) #['a1c', 'aac', 'a c', 'a\nc']
# []代表匹配一个字符,这一个字符是来自于我们自定义的范围
1.print(re.findall('a[0-9]c','a,c a a1c a9c aaac a c asfdsaf a\nc',re.DOTALL)) #['a1c', 'a9c']2.print(re.findall('a[a-zA-Z]c','a,c aAc a1c a9c aaac a c asfdsaf a\nc',re.DOTALL)) #['aAc', 'aac']3.print(re.findall('a[a-zA-Z]c','a,c aAc a1c a9c aaac a c asfdsaf a\nc',re.DOTALL)) #['aAc', 'aac']4.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', 'a-c', 'a*c', 'a/c']5.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', 'a-c', 'a*c', 'a/c']6.print(re.findall('a[^0-9]c','a,c a a1c a9c aaac a c asfdsaf a\nc',re.DOTALL)) #['a,c', 'aac', 'a c', 'a\nc']# ?代表左边那一个字符出现0次到1次
print(re.findall('ab?','a ab abb abbbb a123b a123bbbb')) #['a', 'ab', 'ab', 'ab', 'a', 'a']
# *代表左边那一个字符出现0次到无穷次
print(re.findall('ab*','a ab abb abbbb a123b a123bbbb')) #['a', 'ab', 'abb', 'abbbb', 'a', 'a']
# +代表左边那一个字符出现1次到无穷次
print(re.findall('ab+','a ab abb abbbb a123b a123bbbb')) #['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')) #['ab', 'abb', 'abbbb']
print(re.findall('ab+','a ab abb abbbb a123b a123bbbb')) #['ab', 'abb', 'abbbb']
print(re.findall('ab{0,}','a ab abb abbbb a123b a123bbbb')) #['a', 'ab', 'abb', 'abbbb', 'a', 'a']
print(re.findall('ab*','a ab abb abbbb a123b a123bbbb')) #['a', 'ab', 'abb', 'abbbb', 'a', 'a']
print(re.findall('ab{3}','a ab abb abbbb a123b a123bbbb')) #['abbb']# *匹配任意0个到无穷个字符,贪婪匹配
print(re.findall('a.*c','a123213123asdfasdfc123123123123+-0)((c123123')) #['a123213123asdfasdfc123123123123+-0)((c']# *? 匹配任意0个到无穷个字符,非贪婪匹配
print(re.findall('a.*?c','a123213123asdfasdfc123123123123+-0)((c123123'))#['a123213123asdfasdfc']# |或者
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')) #['companies', '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>')) #['https://www.douniwan.com/1.mp4', 'https://www.xxx.com/2.mp4']print(re.findall('a\\\\c','a\c aac')) #['a\\c']
print(re.findall(r'a\\c','a\c aac')) #['a\\c']#使匹配对大小写不敏感
print(re.findall('alex','my name is alex Alex is dsb aLex ALeX',re.I)) #['alex', 'Alex', 'aLex', 'ALeX']#匹配每行行末
msg="""
my name is egon
asdfsadfadfsadf egon
123123123123123egon
"""
print(re.findall('egon$',msg,re.M)) #['egon', 'egon', 'egon'] #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) [('href', 'https://www.douniwan.com/1.mp4'), ('href', 'https://www.xxx.com/2.mp4')]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) #<_sre.SRE_Match object; span=(14, 51), match='href="https://www.douniwan.com/1.mp4"'>
print(res.group(0)) #href="https://www.douniwan.com/1.mp4"
print(res.group(1)) #href
print(res.group(2)) #https://www.douniwan.com/1.mp4res=re.match('abc','123abc') # 相当于res=re.search('^abc','123abc')
print(res) Nonepattern=re.compile('alex')
print(pattern.findall('alex is alex is alex')) #['alex', 'alex', 'alex']
print(pattern.search('alex is alex is alex')) #<_sre.SRE_Match object; span=(0, 4), match='alex'>
print(pattern.match('alex is alex is alex')) #<_sre.SRE_Match object; span=(0, 4), match='alex'>
示范
#将数字全部提取出来
import re
msg="1-2*(60+(-40.35/5)-(-40*3))"
print(re.findall('\D?(-?\d+\.?\d*)',msg))
二、hashlib模块
什么是hash:hash是一种算法,该算法接受一系列的数据,经过运算会得到一个hash值
hash值具备三大特性:
- 只要传入的内容一样,那么得到的hash值一定是一样
- 只要采用hash算法固定,无论传入的内容多大,hash值的长度是固定
- hash值不可逆,即不能通过hash值逆推出内容
为何要用hash:文件完整性校验
md5:
import hashlibm = hashlib.md5() m.update('你好'.encode('utf-8')) m.update('hello'.encode('utf-8')) print(m.hexdigest()) #65c83c71cb3b2e2882f99358430679c3 print(len(m.hexdigest())) #32位
sha:
import hashlibm2 = hashlib.sha512() m2.update(b'asdfassssssssssssssssssssssssssss') print(m2.hexdigest()) print(len(m2.hexdigest())) #128位
查看文件文件的hash
import hashlib with open('a.txt', mode='rb') as f:m = hashlib.md5()for line in f:m.update(line)print(m.hexdigest())
hash 加盐:
import hashlibpwd = 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())
python-re模块-hashlib模块相关推荐
- python包mdure_Python hashlib模块实例使用详解
这篇文章主要介绍了Python hashlib模块实例使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 hashlib模块主要的作用: 加密保 ...
- python学习笔记——hashlib模块
上篇:https://blog.csdn.net/qq_42489308/article/details/89813895 hashlib Hash,译做"散列",也有直接音译为& ...
- python hashlib_python中hashlib模块用法示例
我们以前介绍过一篇Python加密的文章:Python 加密的实例详解.今天我们看看python中hashlib模块用法示例,具体如下. hashlib hashlib主要提供字符加密功能,将md5和 ...
- python hashlib_python之hashlib模块
hashlib模块是python内置的摘要算法. hashlib有两种调用方式: 第一种是hashlib.new方法 new(name, data=b'', **kwargs) - returns a ...
- python中的hashlib模块
1 hashlib模块的作用: hashlib是一个提供字符串加密功能的模块,包含MD5和SHA的算法,MD5和SHA是摘要算法,摘要算法是什么呢: 也可以称为哈希算法,离散算法.通过一个函数将任意长 ...
- 4-20模块 序列化模块 hashlib模块
1,模块,py文件就是模块,py之所以好用就是模块多. 2,模块的分类: 1,内置模块,python 安装时自带的模块 2,扩展模块,别人写好的,需要安装之后,可以直接使用.itchat微信模块, b ...
- addsectionpic.java_python基础21——json/piclkle模块configparser模块hashlib模块subprocess模块...
json与pickle模块 1.什么是序列化&反序列化 内存中的数据类型---->序列化---->特定的格式(json格式或者pickle格式) 内存中的数据类型 土办法: {'a ...
- 模块 hashlib模块
hashlib模块 提供摘要算法 主要做对比,比较两段代码是否完全一致 不管算法多么不同,摘要功能始终不变, 对同一个字符串进项同一算法摘要得到的值始终不变 MD5值的生成 import hashl ...
- 7.19 包 logging模块 hashlib模块 openpyxl模块 深浅拷贝
包 包是什么 他是一系列文件的结合体,表现形式就是文件夹 包的本质还是模块 他通常会有__init__.py文件 我们首先回顾一下模块导入的过程 import module首次导入模块(.py文件) ...
最新文章
- 规范的 Commit Message
- 零基础怎么学UI设计
- 从分治算法到 MapReduce
- [怪谈]唯有数学不会因时代的变迁而没落
- 深度案例 | 微车:数据驱动价值,建立 1.3 亿车主的汽车生活平台
- Java笔记(3) - 使用Spring Cloud Zookeeper + Feign实现服务发现
- 好代码是管出来的——使用Git来管理源代码
- Oracle-11g-R2 RAC 环境下 GPnP Profile 文件
- Cilium创建pod network源码解析
- linux 集群服务器设置,Zookeeper集群配置
- 云之讯-》返回码105147
- 贪心(百练1328):安放雷达(区间问题)
- 云之讯融合通讯开放平台_提供融合语音,短信,VoIP,视频和IM等通讯API及SDK。...
- C语言m++与++m
- 皇甫懒懒 华清远见Java学习笔记-身份证校验
- 项目经理面对项目陷困境该这样采取措施
- 【MoveIt】入门教程-第一章(上)Move Group C++ Interface(官方教程翻译+个人补充)
- 毕设进度跟踪之开题报告
- 企业生产经营相关英文及缩写之(1)--供应链/物料控制
- 性能远超小型机?新华三推出最新HPE Superdome Flex 280服务器