一.re模块

1.什么是正则?

正则就是用一系列具有特殊含义的字符组成一套规则,该规则用来描述具有某一特征的字符串,
正则就是用来去一个大的字符串中匹配出符合规则的子字符串

2.为何要用正则?

用户注册,爬虫程序

3.常用方法:

import re

print(re.findall('\w','Aah123 +-_'))
#['A', 'a', 'h', '1', '2', '3', '_'] print(re.findall('\w9\w','Aa9h123 aaa9c+-_'))
# ['a9h','a9c'] print(re.findall('\W','Aah123 +-_'))
#[' ', '+', '-']
print(re.findall('\s','Aah\t12\n3 +-_'))
#['\t', '\n', ' ']
print(re.findall('\S','Aah\t12\n3 +-_'))
#['A', 'a', 'h', '1', '2', '3', '+', '-', '_']
print(re.findall('\d','Aah\t12\n3 +-_'))
#['1', '2', '3']
print(re.findall('\D','Aah\t12\n3 +-_'))
#['A', 'a', 'h', '\t', '\n', ' ', '+', '-', '_'] print(re.findall('\t','Aah\t12\n3 +-_'))
#['\t']
print(re.findall('\n','Aah\t12\n3 +-_'))
#['\n'] # ^: 仅从头开始匹配
print(re.findall('^alex','alex is alex is alex'))
#['alex'] # $: 仅从尾部开始匹配
print(re.findall('alex$',' alex is alex is alex'))
['alex'] # .: 代表匹配一个字符,该字符可以是除换行符之外任意字符 #如果后面跟上re.DOTALL就是可以匹配全部任意的字符
print(re.findall('a.c','a a1c aaac a c asfdsaf a\nc',re.DOTALL))
# ['a1c','aac','a c','a\nc'] # []:代表匹配一个字符,这一个字符是来自于我们自定义的范围,[]内的上尖号表示的是取反
print(re.findall('a[0-9]c','a,c a a1c a9c aaac a c asfdsaf a\nc',re.DOTALL))
print(re.findall('a[a-zA-Z]c','a,c aAc a1c a9c aaac a c asfdsaf a\nc',re.DOTALL))
print(re.findall('a[a-zA-Z]c','a,c aAc a1c a9c aaac a c asfdsaf a\nc',re.DOTALL))
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))
print(re.findall('a[+*\-/]c','a,c a+c a-c a*c a/c aAc a1c a9c aaac ac asfdsaf a\nc',re.DOTALL))
print(re.findall('a[^0-9]c','a,c a a1c a9c aaac a c asfdsaf a\nc',re.DOTALL)) # 重复匹配
# ?:代表左边那一个字符出现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'))
print(re.findall('ab+','a ab abb abbbb a123b a123bbbb')) print(re.findall('ab{0,}','a ab abb abbbb a123b a123bbbb'))
print(re.findall('ab*','a ab abb abbbb a123b a123bbbb')) print(re.findall('ab{3}','a ab abb abbbb a123b a123bbbb')) # .*: 匹配任意0个到无穷个字符,贪婪匹配
print(re.findall('a.*c','a123213123asdfasdfc123123123123+-0)((c123123')) #.*?:匹配任意0个到无穷个字符,非贪婪匹配
print(re.findall('a.*?c','a123213123asdfasdfc123123123123+-0)((c123123')) #|:或者
print(re.findall('companies|company','Too many companies have gone bankrupt,c and the next one is my company'))
#忽略大小写
print(re.findall('alex','my name is alex Alex is dsb aLex ALeX',re.I)) msg="""
my name is egon
asdfsadfadfsadf egon
123123123123123egon
"""
print(re.findall('egon$',msg,re.M)) #my name is egon\nasdfsadfadfsadf egon\n123123123123123egon' #\# print(re.findall('a\\c','a\c')) #对于正则来说a\\c确实可以匹配到a\c,但是在python解释器读取a\\c时,会发生转义,然后交给re去执行,所以抛出异常
print(re.findall(r'a\\c','a\c')) #r代表告诉解释器使用rawstring,即原生字符串,把我们正则内的所有符号都当普通字符处理,不要转义
print(re.findall('a\\\\c','a\c')) #同上面的意思一样,和上面的结果一样都是['a\\c'] #():分组
print(re.findall('ab+','ababab123')) #['ab', 'ab', 'ab']
print(re.findall('(ab)+123','ababab123')) #['ab'],匹配到末尾的ab123中的ab
print(re.findall('(?:ab)+123','ababab123')) #findall的结果不是匹配的全部内容,而是组内的内容,?:可以让结果为匹配的全部内容
print(re.findall('href="(.*?)"','<a href="http://www.baidu.com">点击</a>'))#['http://www.baidu.com']
print(re.findall('href="(?:.*?)"','<a href="http://www.baidu.com">点击</a>'))#['href="http://www.baidu.com"']
#['1', '2', '60', '-40.35', '5', '-4', '3']取出其中的数字
msg="1-2*(60+(-40.35/5)-(-40*3))"
print(re.findall('\D?(-?\d+\.?\d*)',msg))

re模块的其他方法:

res=re.search()  #只匹配成功一次,就结束,res.group() 取其中的分组,如果不写,就全取,如果写参数,就只取参数所对应的那个组

res=re.match()  #只从开头进行匹配,相当于在search中的正则表达式中加 上尖号

# 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) # 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)
# print(res.group(0))
# print(res.group(1))
# print(res.group(2)) # res=re.match('abc','123abc') ## res=re.search('^abc','123abc')
# print(res) # print(re.findall('alex','alex is alex is alex'))
# print(re.search('alex','alex is alex is alex'))
# print(re.match('alex','alex is alex is alex')) # 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'))

二.hashlib模块

1.什么是hash?

hash是一种算法,该算法接受一系列的数据,经过运算会得到一个hash值。

hash值具备三大特性:

1.传入的内容一样,得到的hash值一定一样,换句话说hash值一样,内容就一样。

2.如果采用的hash算法固定,无论传入的内容多大,hash值的长度是固定的。

3.hash值不可逆,就是说不能通过hash值逆推出内容。

2.为何要用hash?

特性1+2=>文件完整性的校验

特性3=>用于加密

import hashlib

m=hashlib.md5()
m.update('你好'.encode('utf-8'))
m.update('hello'.encode('utf-8'))
print(m.hexdigest()) #65c83c71cb3b2e2882f99358430679c3 m1=hashlib.md5()
m1.update('你好hello'.encode('utf-8'))
print(m1.hexdigest()) #65c83c71cb3b2e2882f99358430679c3
print(len(m1.hexdigest())) # m2=hashlib.sha512()
m2.update(b'asdfassssssssssssssssssssssssssss')
print(m2.hexdigest())
print(len(m2.hexdigest()))

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进阶(九)----json模块, pickle模块, os模块,sys模块,hashlib模块

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

  3. 4-20模块 序列化模块 hashlib模块

    1,模块,py文件就是模块,py之所以好用就是模块多. 2,模块的分类: 1,内置模块,python 安装时自带的模块 2,扩展模块,别人写好的,需要安装之后,可以直接使用.itchat微信模块, b ...

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

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

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

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

  6. Python3 os模块&sys模块&hashlib模块

    ''' os模块 非常重要的模块 ''' import os # print(os.getcwd()) # 获取当前工作目录 # os.chdir(r'路径名') # 改变当前工作目录 # print ...

  7. 0420模块 序列化模块 hashlib模块

    复习:内置方法 __len__ len(obj)的结果依赖于obj.__len__()的结果,计算对象的长度__hash__ hash(obj)的结果依赖于obj.__hash__()的结果,计算对象 ...

  8. day13 函数模块之序列化 random 模块 os模块 sys模块 hashlib模块 collections模块

    json import json dic = {'k1':'v1','k2':'v2','k3':'v3'} str_dic = json.dumps(dic) #序列化:将一个字典转换成一个字符串 ...

  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. LeetCode(112):路径总和

    Easy! 题目描述: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及 ...

  2. 《剑指offer》从上往下打印二叉树

    本题来自<剑指offer> 从上往下打印二叉树 题目: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路: 队列的思想. 先将根节点加入,当取该节点时候,依次将左右子树加入,直 ...

  3. C#概念总结(三)

    1.定义结构体 定义了结构体,必须使用了stuct语句,struct定义了一个带有多个成员的的新数据类型.C# 的结构不同于C的.具有一下等特点: 结构可以有方法.字段.索引.属性.运算方法和事件.结 ...

  4. java概念基础笔记整理

    1.构造方法没有类型,有类型的不是不叫构造方法. 2.一个类的的成员变量可以是java允许的任何数据类型,一个类可以把某个对象作为自己的一个成员变量,如果用这样的类创建对象,那么该对象中就会其他对象, ...

  5. C++ StrCat()

    关于StrCat function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/bb759925(v=vs.85).aspx ...

  6. CPU虚拟化

    1. 为什么需要 CPU 虚拟化 X86 操作系统是设计在直接运行在裸硬件设备上的,因此它们自动认为它们完全占有计算机硬件.x86 架构提供四个特权级别给操作系统和应用程序来访问硬件.  Ring 是 ...

  7. python列表1

    List (列表)List(列表) 是 Python 中使用最 频繁的数据类 型.列表 可以 完成大 多数集 合类 的数据 结构 实现. 列表中 元素 的类型 可以 不相同 ,它支 持数 字,字 符串 ...

  8. 目标检测中的mAP

    一.IOU的概念 交集和并集的比例(所谓的交集和并集,都是预测框和实际框的集合关系).如图: 二.Precision(准确率)和Recall(召回率)的概念 对于二分类问题,可将样例根据其真实类别和预 ...

  9. 20165323 2017-2018-2 《Java程序设计》课程总结

    一.每周作业链接汇总 预备作业1:20165323 我期望的师生关系 预备作业2:20165323 学习基础与C语言学习心得 预备作业3:20165323 预备作业三 第一周作业:20165323&l ...

  10. error: each element of 'ext_modules' option must be an Extension instance or 2-tuple

    在编译cython扩展时出现. 解决办法: 必须先import setup再import extension,否则报错 from setuptools import setup from distut ...