re模块(正则)
一, 什么是正则?
正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.
在python中,正则内嵌在python中,并通过re模块实现,正则表达模式被编译成一系列的字节码,然后由c编写的匹配引擎执行.
二,常用的匹配模式(元字符)
import re
# 将所有的数据都找出来放进列表中list,一一匹配
print(re.findall('alex', 'haha alex is alex is dsb'))
# >>>: ['alex', 'alex'] # \w 匹配字母数字及下划线(一个\w每次匹配一个字符)
# \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'] # \s 匹配任意空白字符,等价于\t\n\r\f
# \S 匹配非空字符 # \d 匹配任意数字,[0-9]
# \D 匹配任意非数字 # ^ : 仅从开头开始匹配
# $ : 仅从尾部开始匹配
print(re.findall('^alex', 'alex is alex is alex'))
# >>>: ['alex']
print(re.findall('^alex', '1alex is alex is alex'))
# >>>: []
重复匹配: | . | * | ? | .* | .*? | + | {n,m}
# . :代表一个字符,该字符可以是任意字符(除换行符)
print(re.findall('a.c', 'a alc aaac a c asfdsaf'))
# >>>: ['alc', 'aac', 'a c'] print(re.findall('a.c', 'a alc aaac a\nc asfd',re.DOTALL)) #DOTALL使得.匹配包括换行符在内的所有字符
# >>>: ['alc', 'aac', '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']
# .* :匹配任意0个到无穷个字符,贪婪匹配
print(re.findall('a.*c','a132142qwdcavcccc(((()))))c2333'))
# >>>: ['a132142qwdcavcccc(((()))))c']
# .*? :匹配任意0个到无穷个字符,非贪婪匹配
print(re.findall('a.*?c', 'a132142qwdcavcccc(((()))))c2333'))
# >>>: ['a132142qwdc', 'avc']
# |:或者
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('a\\\\c','a\c aac'))
print(re.findall(r'a\\c','a\c aac'))
# >>>: ['a\\c']
# 忽略大小写
# print(re.findall('alex','my name is alex ALex is dSB',re.I))
# # >>>: ['alex', 'ALex'] # msg = '''my name is egon
# asdfassg egon
# 122324324egon'''
# print(re.findall('egon$',msg,re.M))
# >>>: ['egon', 'egon', 'egon']
# []: 代表匹配一个字符,这个字符是来自于自定义的范围
print(re.findall('a[1]c', 'a a1c aaac a c asfdsaf',re.DOTALL))
# >>>: ['a1c']
print(re.findall('a[0-9]c', 'a a1c aaac a7c asfdsaf',re.DOTALL)) #[0-9]的数字
# >>>: ['a1c', 'a7c']
print(re.findall('a[a-zA-Z]c', 'a a1c aaac a7c asfdsaf',re.DOTALL)) #所有字母
# >>>: ['aac']
print(re.findall('a[+*/-]c', 'a a1c aaac a7c asfdsaf',re.DOTALL)) #-代表连字符,在首尾才表示符号意思
# 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) 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)) #运行结果
[('href', 'https://www.douniwan.com/1.mp4'), ('href', 'https://www.xxx.com/2.mp4')]
<_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.mp4
re模块(正则)的更多相关文章
- Python全栈 正则表达式(re模块正则接口全方位详解)
re模块是Python的标准库模块 模块正则接口的整体模式 re.compile 返回regetx对象 finditer fullmatch match search 返回 match对象 match ...
- Python使用re模块正则式的预编译及pickle方案
项目上线要求当中有言论和昵称的过滤需求, 客户端使用的是python脚本, python脚本中直接利用re模块来进行正则匹配, 一开始的做法是开启游戏后, 每帧编译2条正则式, 无奈运营需求里面100 ...
- python,re模块正则
python没有正则需要导入re模块调用.正则表达式是为了匹配字符串,动态模糊的匹配,只要有返回就匹配到了, 没返回就没匹配到,前面是格式后面是字符串 最常用的匹配语法: re.match()#麦驰, ...
- 反射,hashlib模块,正则匹配,冒泡,选择,插入排序
一.反射(自省) 首先通过一个例子来看一下本文中可能用到的对象和相关概念. import sys # 模块,sys指向这个模块对象import inspectdef foo(): pass # 函数, ...
- 常用模块-正则re
常用模块之正则模块 """ 正则表达式与re模块的关系 1.正则表达式是一门独立的技术,任何语言均可使用 2.python中要想使用正则表达式需要通过re模块 " ...
- python3 re模块正则匹配字符串中的时间信息
匹配时间: # -*- coding:utf-8 -*- import re def parseDate(l): patternForTime = r'(\d{4}[\D]\d{1,2}[\D]\d{ ...
- day19 python之re模块正则练习
1.匹配标签 import re ret = re.search("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>" ...
- Python(re模块,正则)
day18 正则表达式用处? 匹配 字符串 s = 'hello world' print(s.find('llo'))#第一个的位置 ret = s.replace('ll','xx') print ...
- re模块与正则
一.正则 正则就是用来筛选字符串中的特定的内容 正则表达式与re模块的关系: 1.正则表达式是一门独立的技术,任何语言都可以使用 2.python中药想使用正则表达式需要通过调用re模块 正则应用场景 ...
随机推荐
- shell编程 之 ssh远程连接
1,ssh理解 有两个服务器,一个是本地,一个是云端的,都是linux系统的,如果我们想要通过本地访问云端的系统,那我们可以用ssh命令,可以实现本地登入远程连接,上传或者下载文件到远程服务器. ss ...
- Django中的缓存基础知识
由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中,5 ...
- CentOS 7 安装配置 MySQL
https://blog.imzhengfei.com/centos-7-an-zhuang-pei-zhi-mysql/ MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前 ...
- wait/notify实现线程间的通信
使线程之间进行通信之后,系统间的交互性更加强大,在大大提高CPU利用率的同时还会使程序对各线程任务在处理的过程中进行有效的把控与监督. 1.不使用wait/notify实现线程间通信 使用sleep( ...
- 【php】随缘php企业网站管理系统V2.0 shownews.php注入漏洞
程序名称:随缘网络php企业网站管理系统2.0免费版 以下为系统的功能简介: 1.采用div+css布局经测试兼容IE及firefox主流浏览器,其他浏览器暂未测试. 2.产品新闻三级无限分类. 3. ...
- React组件State提升(译)
译自:https://reactjs.org/docs/lifting-state-up.html (适当进行了裁减) 通常我们会碰到这样的情况,当某个组件的state数据改变时,几个React组件同 ...
- NAND Flash底层原理,SLC MLC TLC比较【转】
转自:https://blog.csdn.net/qq_39560607/article/details/81714145 版权声明:请注明转载自Christa_RJ https://blog.csd ...
- valgrind--内存泄漏检测(转)
Valgrind 概述 体系结构 Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核类似于一个框 ...
- ES6学习笔记三(proxy和reflect)
proxy用法 // 代理 { let obj={ time:'2017-03-11', name:'net', _r: }; let monitor=new Proxy(obj,{ // 拦截对象属 ...
- 【转】浅谈Java中的hashcode方法
哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: public native int hashCode(); 根据这个 ...