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 相关文章推荐
Django返回json数据用法示例
Sep 18 Python
详解python开发环境搭建
Dec 16 Python
Python机器学习之K-Means聚类实现详解
Feb 22 Python
使用Python AIML搭建聊天机器人的方法示例
Jul 09 Python
Selenium定位元素操作示例
Aug 10 Python
150行Python代码实现带界面的数独游戏
Apr 04 Python
Tensorflow中批量读取数据的案列分析及TFRecord文件的打包与读取
Jun 30 Python
Python如何进行时间处理
Aug 06 Python
python使用smtplib模块发送邮件
Dec 17 Python
深度学习详解之初试机器学习
Apr 14 Python
python实现简单的名片管理系统
Apr 26 Python
Python破解极验滑动验证码详细步骤
May 21 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强大的时间转换函数strtotime
2016/02/18 PHP
Yii2框架BootStrap样式的深入理解
2016/11/07 PHP
PHP+Session防止表单重复提交的解决方法
2018/04/09 PHP
JavaScript flash复制库类 Zero Clipboard
2011/01/17 Javascript
JQuery each()函数如何优化循环DOM结构的性能
2012/12/10 Javascript
jquery 取子节点及当前节点属性值
2014/07/25 Javascript
JS组件Bootstrap导航条使用方法详解
2016/04/29 Javascript
js 获取站点应用名的简单实例
2016/08/18 Javascript
微信小程序 网络API Websocket详解
2016/11/09 Javascript
基于node.js制作简单爬虫教程
2017/06/29 Javascript
vue.js实现单选框、复选框和下拉框示例
2017/07/18 Javascript
React降级配置及Ant Design配置详解
2018/12/27 Javascript
vue请求本地自己编写的json文件的方法
2019/04/25 Javascript
Layui 数据表格批量删除和多条件搜索的实例
2019/09/04 Javascript
移动端手指操控左右滑动的菜单
2019/09/08 Javascript
jquery 时间戳转日期过程详解
2019/10/12 jQuery
JavaScript 实现同时选取多个时间段的方法
2019/10/17 Javascript
TypeScript之调用栈的实现
2019/12/31 Javascript
Express 配置HTML页面访问的实现
2020/11/01 Javascript
python判断windows隐藏文件的方法
2014/03/21 Python
python django集成cas验证系统
2014/07/14 Python
Windows上使用virtualenv搭建Python+Flask开发环境
2016/06/07 Python
微信 用脚本查看是否被微信好友删除
2016/10/28 Python
Python爬虫之xlml解析库(全面了解)
2017/08/08 Python
python中类和实例如何绑定属性与方法示例详解
2017/08/18 Python
Python使用xpath实现图片爬取
2020/09/16 Python
Python爬虫中Selenium实现文件上传
2020/12/04 Python
CSS3实现苹果手机解锁的字体闪亮效果示例
2021/01/05 HTML / CSS
HTML5 input元素类型:email及url介绍
2013/08/13 HTML / CSS
eBay爱尔兰站:eBay.ie
2019/08/09 全球购物
2014年工作总结及2015工作计划
2014/12/12 职场文书
召开会议通知范文
2015/04/15 职场文书
法定代表人身份证明书
2015/06/18 职场文书
告知书格式
2015/07/01 职场文书
python flask框架快速入门
2021/05/14 Python
详解jQuery的核心函数和事件处理
2022/02/18 jQuery