Python正则re模块使用步骤及原理解析


Posted in Python onAugust 18, 2020

python中使用正则表达式的步骤:

1.导入re模块:import re

2.初始化一个Regex对象:re.compile()

3.刚刚创建的Regex对象调用search方法进行匹配,返回要给March对象

4.刚刚的March对象调用group方法,展示匹配到的字符串

下面例子的知识点:

对正则表达式分组用:(),正则里的分组计数从1开始,不是从0,切记~~

  • group(数字):去对应的分组的值

  • groups():返回所有分组的元组形式

\d表示一个数字

regex_obj = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)')
match_obj = regex_obj.search('我司电话:035-411-1234')
result1 = match_obj.group(1)
result2 = match_obj.group(2)
result3 = match_obj.group(3)
print(result1)
print(result2)
print(result3)
result4 = match_obj.group()
print(result4)
result5 = match_obj.groups()
print(result5)

执行结果:

035
411
1234
035-411-1234
('035', '411', '1234')

补充知识点:\w表示一个单词,\s表示一个空格

regex_obj = re.compile(r'(\d\w\d)-(\d\d\d)-(\d\d\d\d)')
match_obj = regex_obj.search('我司电话:0a5-411-1234')
result = match_obj.group(1)
print(result)
regex_obj = re.compile(r'(\d\w\d)-(\d\d\d)-(\d\d\d\d)')
match_obj = regex_obj.search('我司电话:0哈5-411-1234')
result = match_obj.group(1)
print(result)
regex_obj = re.compile(r'(\d\s\d)-(\d\d\d)-(\d\d\d\d)')
match_obj = regex_obj.search('我司电话:0 5-411-1234')
result = match_obj.group(1)
print(result)

执行结果:

0a5
0哈5
0 5

| 或:

regex_obj = re.compile(r'200|ok|successfully')
match_obj1 = regex_obj.search('vom get request and stored successfully')
result1 = match_obj1.group()
print(result1)
match_obj2 = regex_obj.search('vom get request,response 200 ok')
result2 = match_obj2.group()
print(result2)
match_obj3 = regex_obj.search('vom get request,response ok 200')
result3 = match_obj3.group()
print(result3)

执行结果:

successfully
200
ok

注意:如果search返回的March对象只有一个结果值的话,不能用groups,只能用group

regex_obj = re.compile(r'200|ok|successfully')
match_obj1 = regex_obj.search('vom get request and stored successfully')
result2 = match_obj1.groups()
print(result2)
result1 = match_obj1.group()
print(result1)

执行结果:

()
successfully

? :可选匹配项

+ :1次 或 n次 匹配

* :*前面的字符或者字符串匹配 0次、n次

注意:*前面必须要有内容

 regex_obj = re.compile(r'(haha)*,welcome to vom_admin system') 指haha这个字符串匹配0次或者多次

 regex_obj = re.compile(r'(ha*),welcome to vom_admin system') 指ha这个字符串匹配0次或者多次

. : 通配符,匹配任意一个字符

所以常常用的组合是:.*

regex_obj = re.compile(r'(.*),welcome to vom_admin system')
match_obj1 = regex_obj.search('Peter,welcome to vom_admin system')
name = match_obj1.group(1)
print(name)

执行结果:

Peter

{} : 匹配特定的次数

里面只写一个数字:匹配等于数字的次数

里面写{3,5}这样两个数字的,匹配3次 或 4次 或 5次,按贪心匹配法,能满足5次的就输出5次的,没有5次就4次,4次也没有才是3次

regex_obj = re.compile(r'((ha){3}),this is very funny')
match_obj1 = regex_obj.search('hahahaha,this is very funny')
print("{3}结果",match_obj1.group(1))
regex_obj = re.compile(r'((ha){3,5}),this is very funny')
match_obj1 = regex_obj.search('hahahaha,this is very funny')
print("{3,5}结果",match_obj1.group(1))

执行结果:

{3}结果 hahaha
{3,5}结果 hahahaha

findall():返回所有匹配到的字串的列表

regex_obj = re.compile(r'\d\d\d')
match_obj = regex_obj.findall('我是101班的,小李是103班的')
print(match_obj)
regex_obj = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)')
match_obj = regex_obj.findall('我家电话是123-123-1234,我公司电话是890-890-7890')
print(match_obj)

打印结果:

['101', '103']
[('123', '123', '1234'), ('890', '890', '7890')]

[]:创建自己的字符集:

[abc]:包括[]内的字符

[^abc]:不包括[]内的所有字符

也可以使用:[a-zA-Z0-9]这样简写

regex_obj = re.compile(r'[!@#$%^&*()]')
name = input("请输入昵称,不含特殊字符:")
match_obj = regex_obj.search(name)
if match_obj:
  print("昵称输入不合法,包含了特殊字符:", match_obj.group())
else:
  print("昵称有效")

执行结果:

请输入昵称,不含特殊字符:*h
昵称输入不合法,包含了特殊字符: *

 ^:开头

 $:结尾 

regex_obj = re.compile(r'(^[A-Z])(.*)')
name = input("请输入昵称,开头必须大写字母:")
match_obj = regex_obj.search(name)
print(match_obj.group())

执行结果:

请输入昵称,开头必须大写字母:A1234
A1234

sub():第一个参数为要替换成的,第二个参数传被替换的,返回替换成功后的字符串

regex_obj = re.compile(r'[!@#$%^&*()]')
match_obj = regex_obj.sub('嘿','haha,$%^,hahah')
print(match_obj)

执行结果:

haha,嘿嘿嘿,hahah

补充一下正则表达式的表,正则太复杂了,要常看常用才能熟练

Python正则re模块使用步骤及原理解析

Python正则re模块使用步骤及原理解析

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python登陆asp网站页面的实现代码
Jan 14 Python
python 实现红包随机生成算法的简单实例
Jan 04 Python
Python中字符串格式化str.format的详细介绍
Feb 17 Python
深入理解Python中的super()方法
Nov 20 Python
Python使用re模块实现信息筛选的方法
Apr 29 Python
Python使用pymongo模块操作MongoDB的方法示例
Jul 20 Python
pyqt5 使用label控件实时显示时间的实例
Jun 14 Python
Python二进制文件读取并转换为浮点数详解
Jun 25 Python
python+selenium+chrome批量文件下载并自动创建文件夹实例
Apr 27 Python
python实现自动化群控的步骤
Apr 11 Python
tensorflow+k-means聚类简单实现猫狗图像分类的方法
Apr 28 Python
Django给表单添加honeypot验证增加安全性
May 06 Python
python使用建议技巧分享(三)
Aug 18 #Python
python3跳出一个循环的实例操作
Aug 18 #Python
OpenCV图片漫画效果的实现示例
Aug 18 #Python
Django DRF APIView源码运行流程详解
Aug 17 #Python
Django CBV模型源码运行流程详解
Aug 17 #Python
无惧面试,带你搞懂python 装饰器
Aug 17 #Python
Python Request类源码实现方法及原理解析
Aug 17 #Python
You might like
用PHP和ACCESS写聊天室(二)
2006/10/09 PHP
一段防盗连的PHP代码
2006/12/06 PHP
发布一个用PHP fsockopen写的HTTP下载的类
2007/02/22 PHP
PHP把数字转成人民币大写的函数分享
2014/06/30 PHP
PHP模块memcached使用指南
2014/12/08 PHP
PHP 实现页面静态化的几种方法
2017/07/23 PHP
Yii2.0多文件上传实例说明
2017/07/24 PHP
快速解决PHP调用Word组件DCOM权限的问题
2017/12/27 PHP
JavaScript性能优化 创建文档碎片(document.createDocumentFragment)
2010/07/13 Javascript
event.X和event.clientX的区别分析
2011/10/06 Javascript
JavaScript基本编码模式小结
2012/05/23 Javascript
js实现仿MSN带关闭功能的右下角弹窗代码
2015/09/04 Javascript
JS跨域交互(jQuery+php)之jsonp使用心得
2016/07/01 Javascript
完美解决jQuery符号$与其他javascript 库、框架冲突的问题
2016/08/09 Javascript
原生js 封装get ,post, delete 请求的实例
2017/08/11 Javascript
vue组件实现文字居中对齐的方法
2017/08/23 Javascript
基于jQuery Ajax实现下拉框无刷新联动
2017/12/06 jQuery
vue 动态修改a标签的样式的方法
2018/01/18 Javascript
python中getattr函数使用方法 getattr实现工厂模式
2014/01/20 Python
Python 抓取动态网页内容方案详解
2014/12/25 Python
Python除法之传统除法、Floor除法及真除法实例详解
2019/05/23 Python
Python rabbitMQ如何实现生产消费者模式
2020/08/24 Python
Python __slots__的使用方法
2020/11/15 Python
基于注解实现 SpringBoot 接口防刷的方法
2021/03/02 Python
从一次项目重构说起CSS3自定义变量在项目的使用方法
2021/03/01 HTML / CSS
美国娱乐和流行文化商品店:FYE
2017/09/14 全球购物
Charlotte Tilbury美国官网:英国美妆品牌
2017/10/13 全球购物
美国时尚配饰品牌:Dooney & Bourke
2017/11/14 全球购物
介绍一下Python中webbrowser的用法
2013/05/07 面试题
公司营业员的自我评价
2014/03/04 职场文书
广场舞大赛策划方案
2014/05/31 职场文书
2014年学生管理工作总结
2014/12/20 职场文书
上市公司财务总监岗位职责
2015/04/03 职场文书
节约用电倡议书
2015/04/28 职场文书
2015年班组建设工作总结
2015/05/13 职场文书
音乐会主持人开场白
2015/05/28 职场文书