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模块 正则应用场景 ...
随机推荐
- react实现极简搜索框效果
hover.css内容 * { margin:; padding:; } li.hover { background: #ccc; color: darkgreen; } js内容 import Re ...
- Java开发环境配置(5)--Web 服务器--Tomcat--安装过程遇到的问题
1.参考例子:--- 怎样安装配置tomcat 8_百度经验https://jingyan.baidu.com/article/ff42efa91132a0c19e220208.html 安装与配置T ...
- python初级实战-----关于邮件发送问题
python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需import即可使用.smtplib模块主要负责发送邮件,email模块主要负责构造邮件. sm ...
- jenkins服务器上安装配置Android SDK
1.下载Android SDK http://tools.android-studio.org/index.php/sdk/ 我下载的是:android-sdk_r24.4.1-linux.tgz ...
- Liunx之xl2TP的一键搭建
作者:邓聪聪 1 L2TP(Layer 2 Tunnel Protocol二层隧道协议l),上图说明了VPN的一些特点,出差员工或者外出员工通过拨特定号码的方式接入到企业内部网络; --------- ...
- aix装python
网址是:http://www-03.ibm.com/systems/power/software/aix/linux/toolbox/alpha.html 在AIX下安装python Python是个 ...
- mysql连表分组报错---- sql_mode=only_full_group_by问题解决
#### sql语句报错问题 #1055 - Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggreg ...
- Java中常用的加密算法小结
散列算法(单向散列,不可逆) MD5(Message Digest Algorithm 5) SHA(Secure Hash Algorithm) 对称加密(加密解密使用同一密钥,速度快) D ...
- postgresql 触发器 更新操作
1 前言 功能需求:当一张表格某个字段变化,另一张表某个字段写入该值 2 代码 CREATE OR REPLACE FUNCTION "public"."synStatu ...
- 将list集合转json
public static class DataHelper { /// /// js 序列化器 /// static JavaScriptSerializer jss = new JavaScrip ...