Python中正则表达式的用法总结
作者:topleeyap 时间:2021-04-23 17:04:49
正则表达式很神奇啊
# -*- coding:utf-8 -*-
import re
def print_match_res(res):
"""打印匹配对象内容"""
if res is not None:
print(res.group())
else:
print(None)
# 两种匹配方式:
pattern="[A-Z][a-z]+"
# 一、使用re模块函数进行匹配
res=re.match(pattern,"Tom is a good boy") # 匹配,返回匹配对象
print(type(res))
print(res.group())
# 二、使用预编译后的正则表达式对象的方法进行匹配
obj_pattern=re.compile(pattern) # 预编译,返回正则表达式对象
print(type(obj_pattern))
res=obj_pattern.match("Tom is a good boy") # 匹配,返回匹配对象
print(type(res))
print(res.group())
# 匹配对象的group()和groups()方法
pattern="\d{3}-\d{5}"
obj_pattern=re.compile(pattern)
res=obj_pattern.search("家庭电话:000-88886")
print(res.group()) # 返回整个匹配或特定子组
print(res.groups()) # 返回包含全部子组的元组
# match():从起始部分开始匹配,如果成功,返回匹配对象;失败,返回None。只匹配一次
pattern="my"
# res=re.compile(pattern).match("my name is li")
res=re.match(pattern,"my name is li")
print_match_res(res)
# search(): 从任意位置开始匹配,如果成功,返回匹配对象;失败,返回None。只匹配一次
pattern="my"
# res=re.compile(pattern).search("it's my dog")
res=re.search(pattern,"my name is li")
print_match_res(res)
# 查找全部
# findall(),finditer()
res=re.findall(r"th\w+","This and that",re.I)
print(res)
res=re.finditer(r"th\w+","This and that",re.I)
print(res)
print(next(res).group(),next(res).group())
# 替换
# sub(),subn()
res=re.sub("funny","fool","You are so funny")
print(res)
res=re.subn("funny","fool","You are so funny")
print(res)
# 分割
# splite()
res=re.split("\.","Mr.Smith")
print(res)
print("#"*50)
# 择一匹配符号 a|b
pattern="I|You|She"
res=re.compile(pattern,flags=re.IGNORECASE).match("i love you")
print_match_res(res)
res=re.compile(pattern,flags=re.I).search("who love you")
print_match_res(res)
# 匹配任意单个字符 .
pattern="w{3,}\..+\.com"
res=re.match(pattern,"wwww.google.com/index.html",re.I)
print_match_res(res)
# 字符集 [abc] [a-z0-9]
pattern="[A-Za-z0-9_]*\."
res=re.match(pattern,"Python3.?")
print_match_res(res)
# 特殊字符 \d \w \s \b \\
# 重复 + ? * {N,} {N,M}
# 分组 (...)
pattern="\w+@(\w{1,10}\.)*([a-z]*)"
res=re.match(pattern,"li@gmail.com")
print_match_res(res)
res=re.match(pattern,"li@qq.vip.org")
print_match_res(res)
print(res.group(0),res.group(1),res.group(2),sep="\t")
print(res.groups())
# 匹配字符串的起始和结尾,单词边界 ^a z$ \A \Z \b \B
pattern=r"^the"
# pattern=r"\Athe"
res=re.search(pattern,"The end of the world")
print_match_res(res)
res=re.search(pattern,"they smile")
print_match_res(res)
pattern=r"cry$"
# pattern=r"cry\Z"
res=re.search(pattern,"they cry")
print_match_res(res)
res=re.search(r"\bthe","bit the dog")
print_match_res(res)
res=re.search(r"\Bhe","bit the dog")
print_match_res(res)
来源:https://blog.csdn.net/topleeyap/article/details/78846099
标签:python,正则表达式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php获取当前时间的毫秒数的方法
2023-06-28 06:43:37
Python OpenCV读取png图像转成jpg图像存储的方法
2023-01-07 13:23:56
python 等差数列末项计算方式
2022-01-29 10:12:57
SQL点滴24 监测表的变化
2011-09-30 11:38:41
String 的扩展方法
2008-05-12 22:36:00
PHP页面静态化的优缺点与实现
2023-05-27 09:14:40
Python闭包和装饰器用法实例详解
2021-04-07 10:05:02
tensorflow ckpt模型和pb模型获取节点名称,及ckpt转pb模型实例
2021-05-23 10:22:53
Python基础教程之if判断,while循环,循环嵌套
2023-11-05 12:56:07
使用C#配合ArcGIS Engine进行地理信息系统开发
2023-06-25 16:35:01
![](https://img.aspxhome.com/file/2023/6/61966_0s.jpg)
Python中xlsx文件转置操作详解(行转列和列转行)
2022-02-18 03:36:34
![](https://img.aspxhome.com/file/2023/0/92310_0s.jpg)
Python文件操作的方法
2022-10-27 19:54:29
![](https://img.aspxhome.com/file/2023/8/107938_0s.jpg)
实用301转向到另一域名相应页面的asp代码
2011-04-18 10:42:00
Django初步使用Celery处理耗时任务和定时任务问题
2023-10-15 07:41:59
![](https://img.aspxhome.com/file/2023/0/88010_0s.png)
Python利用matplotlib绘制折线图的新手教程
2021-08-18 18:00:01
![](https://img.aspxhome.com/file/2023/5/97265_0s.png)
重新认识CSS的权重
2011-05-24 17:06:00
解决pycharm无法调用pip安装的包问题
2023-01-25 23:43:16
Python实现绘制双柱状图并显示数值功能示例
2023-12-06 06:31:03
![](https://img.aspxhome.com/file/2023/4/101474_0s.png)
2019 Python最新面试题及答案16道题
2022-07-01 03:04:49
Go微服务网关的实现
2023-07-02 22:27:05
![](https://img.aspxhome.com/file/2023/7/87627_0s.png)