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模块 正则应用场景 ...
随机推荐
- CSS-联合选择器
深层布局,逐级进去,指向某一个标签,叫:关联选择器 - 设置嵌套标签的样式 div p {} day02 昨天内容回顾 1.html的操作思想 ** 使用标签把要操作的数据包起来,通过修改标签的属性值 ...
- 【Java编程思想笔记】-集合2
详细的接口API转自博客:https://blog.csdn.net/jyg0723/article/details/80498840#collection-api-%E8%AF%A6%E8%A7%A ...
- 20165337学习基础和C语言基础调查
20165337学习基础和C语言基础调查 你有什么技能比大多人(超过90%以上)更好? 讲道理我感觉我自己没有什么能比90%以上的人都做得好的技能,我就瘸子里面拔将军挑一个我自认为还不错的技能吧. 我 ...
- python小练习,利用dict,做一个简单的登录。
'''利用字典实现登录'''users=[{'username':'jerry','pwd':'123456'},{'username':'tom','pwd':'1'}] def login(use ...
- OGG选择捕捉和应用模式
本章包含的信息可帮助您确定适用于数据库环境的捕获和应用模式. 主题: Oracle GoldenGate捕获和应用进程概述 决定使用哪种捕捉方法 决定使用哪种应用方法 同时使用不同的捕捉和应用模式 切 ...
- Android 正则表达式验证手机号码
方案一:比较精准的判断手机段位,但是随着手机号段的增多要不断的修改正则 public boolean isPhoneNumber1(String phone) { String regExp = &q ...
- 【MongoDB】 Failed to connect to 127.0.0.1:27017, reason: Connection refused
由于项目需要,在一台虚拟机上安装了MongoDB,但是在启动的时候,出现如下错误: [root@localhost bin]# ./mongo MongoDB shell version v3.4.0 ...
- EF Codefirst入门之创建数据库
实验环境是VS 2015.MSSQL Server 2008.windows 10 一.创建项目 通过VS创建一个MVC5项目EntityFrameworkExtension 二.安装Entity F ...
- VS2013中编译openssl的步骤和使用设置
一.VS2013中编译openssl的步骤 版本号:openssl-1.0.1e 1.下载 OpenSSL http://www.openssl.org/,并解压到d:\openssl-1.0.1e目 ...
- 016_把普通用户免秘钥加入root用户的几种方式
一.第一种方式. (1) [root@infra-jyallkv-tikv-pps-7 ~]# tail /etc/sudoers## Allows members of the users grou ...