当前位置:网站首页>打卡第 1 天:正则表达式学习总结
打卡第 1 天:正则表达式学习总结
2022-08-04 21:33:00 【抄代码抄错的小牛马】

活动地址:CSDN21天学习挑战赛
目录
前言:
学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天多一天平庸的就困扰。
正则表达式描述了一种字符串匹配的模式
通过使用正则表达式,可以:
- 测试字符串内的模式。
例如,可以测试输入字符串,以查看字符串内是否出现电话号码模式或信用卡号码模式。这称为数据验证。 - 替换文本。
可以使用正则表达式来识别文档中的特定文本,完全删除该文本或者用其他文本替换它。 - 从字符串中提取子字符串。
可以查找文档内或输入域内特定的文本。
re库常用的功能函数

Match对象的属性:

match对象的方法:

re.match()
>>> import re
>>> result = re.match("itcast","itcast.cn")
>>> result.group()
'itcast'
不是以4 ,7结尾的手机号码(11位)
import re
tels = ["13100001234", "18912344321", "10086", "18800007777"]
for tel in tels:
ret = re.match("1\d{9}[0-35-68-9]", tel)
if ret:
print(ret.group())
else:
print("%s 不是想要的手机号" % tel)
re.search()
import re
line = "Cats are smarter than dogs"
searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I)
if searchObj:
print ("searchObj.group() : ", searchObj.group())
print ("searchObj.group(1) : ", searchObj.group(1))
print ("searchObj.group(2) : ", searchObj.group(2))
else:
print ("Nothing found!!")
re.findall()
>>> import re
>>> ret = re.findall(r"\d+", "python = 9999, c = 7890, c++ = 12345")
>>> print(ret)
['9999', '7890', '12345']
re.sub()
>>> import re
>>>
>>> phone = "2004-959-559 # 这是一个电话号码"
>>>
>>> # 删除注释
>>> num = re.sub(r'#.*$', "", phone)
>>> print ("电话号码 : ", num)
电话号码 : 2004-959-559
>>>
>>> # 移除非数字的内容
>>> num = re.sub(r'\D', "", phone)
>>> print ("电话号码 : ", num)
电话号码 : 2004959559
re.split()
以 :或 空格 切割
>>> import re
>>> ret = re.split(r":| ","info:xiaoZhang 33 shandong")
>>> print(ret)
['info', 'xiaoZhang', '33', 'shandong']
边栏推荐
- JdbcTemplate概述和测试
- webmine网页挖矿木马分析与处置
- 可视化工作流引擎开发OA系统,让企业少花冤枉钱
- 中大型商业银行堡垒机升级改造方案!必看!
- 路由中的meta、params传参的一些问题(可传不可传,为空,搭配,点击传递多次参数报错)
- 如何一键重装Win11系统 一键重装系统方法
- 基于 Milvus 和 ResNet50 的图像搜索(部署及应用)
- 【uiautomation】微信好友列表获取(存储到txt中)
- AtCoder Beginner Contest 262 D - I Hate Non-integer Number
- ue unreal 虚幻 高分辨率无缩放 编辑器字太小 调整编辑器整体缩放
猜你喜欢
![[2022 Nioke Duo School 5 A Question Don't Starve] DP](/img/fa/f1d11297cc5f58919bcc579f0a82e9.png)
[2022 Nioke Duo School 5 A Question Don't Starve] DP

MySQL查询为啥慢了?

Spss-一元回归实操

SPSS-unary regression practice

js数据类型、节流/防抖、点击事件委派优化、过渡动画

How to understand the crawler's Scrapy framework in the simplest and most popular way?

In action: 10 ways to implement delayed tasks, with code!
![[2022 Hangzhou Electric Multi-School 5 1003 Slipper] Multiple Super Source Points + Shortest Path](/img/78/054329dec6a6faea5e9d583b6a8da5.png)
[2022 Hangzhou Electric Multi-School 5 1003 Slipper] Multiple Super Source Points + Shortest Path

Develop your own text recognition application with Tesseract

基于 Milvus 和 ResNet50 的图像搜索(部署及应用)
随机推荐
unity2D横版游戏教程9-对话框dialog
DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基一种疏水18碳磷脂
Data warehouse (1) What is data warehouse and what are the characteristics of data warehouse
[Teach you to use the serial port idle interrupt of the STM32HAL library]
[Linear Algebra 03] Elimination method display and 4 solutions of AX=b
EasyGBS接入最新版海康摄像头后无法传递告警信息该如何解决?
国内的PMP证书含金量到底如何
SPSS-System Clustering Hand Calculation Practice
Ramnit感染型病毒分析与处置
LayaBox---knowledge point
【CC3200AI 实验教程 1】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-开发环境搭建
boostrap多选PID查找端口 window
MySQL查询为啥慢了?
[2022 Hangzhou Electric Power Multi-School 5 1012 Questions Buy Figurines] Application of STL
[2022 Hangzhou Electric Multi-School 5 1003 Slipper] Multiple Super Source Points + Shortest Path
How to solve the problem that the alarm information cannot be transmitted after EasyGBS is connected to the latest version of Hikvision camera?
webmine网页挖矿木马分析与处置
matlab drawing
visual studio 2015 warning MSB3246
OD-Model【6】:YOLOv2