Python正则表达式与hashlib模块
菜鸟学python第十六天
1.re模块(正则表达式)
什么是正则表达式
正则表达式是一个由特殊字符组成的序列,他能帮助对字符串的某种对应模式进行查找。
在python中,re 模块使其拥有全部的正则表达式功能。
re模块的使用
re正则表达式的运行机制: 一个一个进行比对
re.findall()
import re
# \w, 匹配字母数字下划线
print(re.findall('\w', 'Aah123 +-_')) # \w, ['A', 'a', 'h', '1', '2', '3', '_']
print(re.findall('\w\w', 'Aah123 +-_')) # \w\w, ['Aa', 'h1', '23']
print(re.findall('\w9\w', 'Aa9h123 +-_ aaa9--')) # \w9\w ,['a9h']
# \W :匹配非字母数字下划线
print(re.findall('\W', 'hshk _=fh**#_ ')) # \W,[' ', '=', '*', '*', '#', ' ', ' ']
# \s : 匹配任意空白字符(\t\s\n都包含在内)
print(re.findall('\s', 'dhfgk\tgds\n\g_jflkds ')) # ['\t', '\n', ' ', ' ']
# ^str: 从头开始匹配
print(re.findall('^alex', 'hahahaha alex is alex is dsb')) # d第一位没查找到返回空
# $: 从尾开始匹配
print(re.findall('Jason$', 'hello Jason')) # ['Jason']
# .:可以匹配除换行符之外的任意字符,当re.DOTALL被指定时,则可匹配包含换行符在内的所有字符
print(re.findall('a.c', 'a alc aaac a c asffgf a\nc')) # ['alc', 'aac', 'a c']
print(re.findall('a.c', 'a alc aaac a c asffgf a\nc', re.DOTALL)) # ['alc', 'aac', 'a c', 'a\nc']
# [] : 代表匹配一个字符,这一个字符是来自于我们自定义的范围
print(re.findall('[1-9]', 's3jl5j6j33j6l')) # ['3', '5', '6', '3', '3', '6']
print(re.findall('[a-z]', 's3jl5j6j33j6l')) # ['s', 'j', 'l', 'j', 'j', 'j', 'l']
print(re.findall('5[a-z]6', 's3jl5j6j33j6l')) # ['5j6']
# 重复匹配
# ? : 代表左边那一个字符出现0次或1次(可有一个或没有的情况)
print(re.findall('ab?', 'a ab aab a123b a123bbbbb')) # ['a', 'ab', 'a', 'ab', 'a', 'a']
# *:代表左边那一个字符出现0次到无穷次(可有可没有的情况)
print(re.findall('ab*', 'a ab abbbb a1243b a123bbb')) # ['a', 'ab', 'abbbb', 'a', 'a']
# +:代表左边那个字符出现一次到无穷次(必须要出现一次以上,否则失败)
print(re.findall('ab+', 'a jab a12b abbbbb ab')) # ['ab', 'abbbbb', 'ab']
# {n,m} : 代表左边那个字符出现n次到m次
print(re.findall('ab{1,3}', 'ab abb abbb aab aaab a b b a')) # ['ab', 'abb', 'abbb', 'ab', 'ab']
print(re.findall('ab{1,}', 'ab abb abbbb aab aaab a b')) # ['ab', 'abb', 'abbbb', 'ab', 'ab']
print(re.findall('ab{3}','a ab abb abbbb a123b a123bbbb')) # ['abbb']
# .*:匹配任意0个到无穷个字符,贪婪匹配(到右边字符最后一次出现时结束)
print(re.findall('a.*b', 'aghfhkdsnb-9=-0b')) # ['aghfhkdsnb-9=-0b']
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">逗你玩呢</a><a href="https://www.xxx.com/2.mp4']
# .*?:匹配任意0个到无穷个字符,非贪婪匹配(到右边字符第一次出现时结束)
print(re.findall('a.*?b', 'aghfhkdsnb-9=-0b')) # ['aghfhkdsnb']
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('ab|ac', 'ab bb ac cc abb acc')) # ['ab', 'ac', 'ab', 'ac']
# ():分组
print(re.findall('a(?:ab|c)', 'aab ac ac ab abbb aaaab')) # ['aab', 'ac', 'ac', 'aab']
print(re.findall('a(ab|c)', 'aab ac ac ab abbb aaaab')) # ['ab', 'c', 'c', 'ab']
# \: 转意
print(re.findall('a\\\\c', 'a\c aac')) # \\\\转以后相当于\\
print(re.findall(r'a\\c', 'a\c aac')) # r表示原生代码,指定python语法不要去识别它
# re.I : 忽略大小写
print(re.findall('alex', 'my name s alex Alex is dsb aLex AleX', re.I)) # ['alex', 'Alex', 'aLex', 'AleX']
# re.M :区分换行符
msg = """
my name is egon
ghkhdsgdskhf egon
hgkshd1243 egon"""
print(re.findall('egon$', msg, re.M)) # $表示从后开始找,re.M表示识别多行
# ['egon', 'egon', 'egon']
re模块其他方法
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) # 匹配成功第一个返回属性及内容,匹配不成功返回none
print(res.group(0)) # 匹配成功返回第一个内容,group将其转换为字符串
print(res.group(1)) # 匹配成功返回分组的第一个内容(href)
print(res.group(2)) # 匹配成功返回分组的第二个内容
输出结果:
<_sre.SRE_Match object; span=(14, 51), match='href="https://www.douniwan.com/1.mp4"'>
href="https://www.douniwan.com/1.mp4"
href
https://www.douniwan.com/1.mp4re.match():从开头查找,开头是则返回该值,不是则返回none
res=re.match('abc','123abc') ## res=re.search('^abc','123abc')
print(res)
输出结果:
Nonere.compile():将要匹配的字符准备好
pattern=re.compile('alex')
print(pattern.findall('alex is alex is alex'))
print(pattern.search('alex is alex is alex'))
print(pattern.match('alex is alex is alex'))
练习
# 提取算式中的数字,包含负数,减号除外
msg = "1-2*(60+(-40.35/5)-(-40*3))"
print(re.findall('\D?(-?\d+\.?\d*)', msg))
输出结果:
['', '', '', '-40.35', '', '-40', '']
2. hashlib 模块
hash:一种算法,用来对数据进行运算校验,返回hash值。
hash三大特性: 1. 只要传入的内容是一样的那返回的hash值一定一样。
2.只要采用的hash算法固定,无论传入多长内容hash值长度不变
3.hash值不可逆,就是不能通过hash值逆推出文件内容
为何要用hash:
1.文件完整性校验(特性1、2)
原理:下载前拿到文件的hash值,下载完成后在目标设备上在进行校验,比对两者是否一致,从而判断文件完整性
方法:将文件以行的形式一行一行读到内存中,加以hash运算最后得到hash值(不能将文件一次性全读到内存中,会导致内存溢出),但是一行行验证当文件行数过多时,效率太低,故我们可以选择几个特征区域加以hash,验证即可。
import hashlib
m=hashlib.md5() # md5 默认hash值为32位
m.update('你好'.encode('utf-8'))
m.update('hello'.encode('utf-8')) # 与m.update('你好hello'encode('utf-8'))de hash值一致
print(m.hexdigest()) # 对以上update文件进行校验返回其hash值
输出结果:
65c83c71cb3b2e2882f99358430679c3
# hash值长度之与算法有关,与文件大小无关
m2=hashlib.sha512() # sha512算法hash值长度为128
m2.update(b'asdfassssssssssssssssssssssssssss')
# print(m2.hexdigest())
print(len(m2.hexdigest()))
输出结果:
128
# 文件验证
with open(r'D:\xxx\xxx\file.txt',mode='rb') as f:
m=hashlib.md5()
for line in f:
m.update(line)
print(m.hexdigest())
2.用来对数据加密(特性3)
原理:在数据的某些位置添加设定数据,将其数据复杂化
# 数据加密
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())
Python正则表达式与hashlib模块的更多相关文章
- python正则表达式之re模块方法介绍
python正则表达式之re模块其他方法 1:search(pattern,string,flags=0) 在一个字符串中查找匹配 2:findall(pattern,string,flags=0) ...
- Python正则表达式与re模块介绍
Python中通过re模块实现了正则表达式的功能.re模块提供了一些根据正则表达式进行查找.替换.分隔字符串的函数.本文主要介绍正则表达式先关内容以及re模块中常用的函数和函数常用场景. 正则表达式基 ...
- python 正则表达式 (重点) re模块
京东的注册页面,打开页面我们就看到这些要求输入个人信息的提示.假如我们随意的在手机号码这一栏输入一个11111111111,它会提示我们格式有误.这个功能是怎么实现的呢?假如现在你用python写一段 ...
- python正则表达式与re模块-02
正则表达式 正则表达式与python的关系 # 正则表达式不是Python独有的,它是一门独立的技术,所有的编程语言都可以使用正则 # 但要在python中使用正则表达式,就必须依赖于python内置 ...
- python 正则表达式re使用模块(match()、search()和compile())
摘录 python核心编程 python的re模块允许多线程共享一个已编译的正则表达式对象,也支持命名子组.下表是常见的正则表达式属性: 函数/方法 描述 仅仅是re模块函数 compile(patt ...
- Python进阶-XVV hashlib模块、configparse模块、logging模块
1.配置相关的configparse模块 配置文件如何组织?python中常见的是将配置文件写成py,然后引入该模块即可.优点是方便访问. 但是也有用类似windows中的ini文件的配置文件,了解即 ...
- python之路----hashlib模块
在平时生活中,有很多情况下,你在不知不觉中,就用到了hashlib模块,比如:注册和登录认证注册和登录认真过程,就是把注册用的账户密码进行:加密 --> 解密 的过程,在加密.解密过程中,用的了 ...
- python学习-59 hashlib模块
hashlib模块 用于加密相关的操作,3.x里代替了md5模块和sha模块 加密功能 import hashlib obj = hashlib.md5() # 如果在md5里加上自己设置的参数,别的 ...
- Python正则表达式与re模块
在线正则表达式测试 http://tool.oschina.net/regex/ 常见匹配模式 模式 描述 \w 匹配字母数字及下划线 \W 匹配非字母数字下划线 \s 匹配任意空白字符,等价于 [\ ...
随机推荐
- laravel-admin 自定义导出表单
官方导出文档 laravel-admin自带的导出excel会导出与此模型关联的其他数据.所以参考官方文档调整代码 文章表:id,title,user_id 用户表:id,username //文章模 ...
- springMVC-RESTful支持
RESTful支持 什么是restful? Restful就是一个资源定位及资源操作的风格.不是标准也不是协议,只是一种风格,是对http协议的诠释. 资源定位:互联网所有的事物都是资源,要求url中 ...
- 使用openssl 生成免费证书
阅读目录 一:什么是openssl? 它的作用是?应用场景是什么? 二:使用openssl生成免费证书 回到顶部 一:什么是openssl? 它的作用是?应用场景是什么? 即百度百科说:openssl ...
- split()分割字符串用法
<script type="text/javascript"> var str="How are you doing today?" documen ...
- npm 修改源地址
修改源地址为淘宝 NPM 镜像 npm config set registry http://registry.npm.taobao.org/ 修改源地址为官方源 npm config set reg ...
- 我的NopCommerce之旅(7): 依赖注入(IOC/DI)
一.基础介绍 依赖注入,Dependency Injection,权威解释及说明请自己查阅资料. 这里简单说一下常见使用:在mvc的controller的构造方法中定义参数,如ICountryServ ...
- C#入门笔记2 变量
变量关系到数据的存储,一个供程序操作的存储区的名字,每一个变量都一个特定的类型,类型决定变量的内存大小和布局. 注:必须一定要先声明,赋值后,才能使用. 变量声明 三种声明方式: 1.先声明,后赋值. ...
- TruncateTable数据库清理
原文引用 https://www.cnblogs.com/liangxiaoking/p/5958456.html 本地链接下载 https://files.cnblogs.com/files/she ...
- vs安装包
http://blog.csdn.net/cometnet/article/details/19551125
- 7、斐波那契数列、跳台阶、变态跳台阶、矩形覆盖------------>剑指offer系列
题目:斐波那契数列 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). f(n) = f(n-1) + f(n-2) 基本思路 这道题在剑指offe ...