当前位置:网站首页>[正则表达式] 匹配分组
[正则表达式] 匹配分组
2022-07-27 14:25:00 【柚子树cc】
1. 匹配分组相关正则表达式
| 代码 | 功能 |
|---|---|
| | | 匹配左右任意一个表达式 |
| (ab) | 将括号中字符作为一个分组 |
\num | 引用分组num匹配到的字符串 |
(?P) | 分组起别名 |
| (?P=name) | 引用别名为name分组匹配到的字符串 |
示例1:|
需求:在列表中[“apple”, “banana”, “orange”, “pear”],匹配apple和pear
import re
# 水果列表
fruit_list = ["apple", "banana", "orange", "pear"]
# 遍历数据
for value in fruit_list:
# | 匹配左右任意一个表达式
match_obj = re.match("apple|pear", value)
if match_obj:
print("%s是我想要的" % match_obj.group())
else:
print("%s不是我要的" % value)
执行结果:
apple是我想要的
banana不是我要的
orange不是我要的
pear是我想要的
示例2:( )
需求:匹配出163、126、qq等邮箱
import re
match_obj = re.match("[a-zA-Z0-9_]{4,20}@(163|126|qq|sina|yahoo)\.com", "[email protected]")
if match_obj:
print(match_obj.group())
# 获取分组数据
print(match_obj.group(1))
else:
print("匹配失败")
执行结果:
[email protected]
163
需求: 匹配qq:10567这样的数据,提取出来qq文字和qq号码
import re
match_obj = re.match("(qq):([1-9]\d{4,10})", "qq:10567")
if match_obj:
print(match_obj.group())
# 分组:默认是1一个分组,多个分组从左到右依次加1
print(match_obj.group(1))
# 提取第二个分组数据
print(match_obj.group(2))
else:
print("匹配失败")
执行结果:
qq
10567
示例3:\num
需求:匹配出hh
match_obj = re.match("<[a-zA-Z1-6]+>.*</[a-zA-Z1-6]+>", "<html>hh</div>")
if match_obj:
print(match_obj.group())
else:
print("匹配失败")
match_obj = re.match("<([a-zA-Z1-6]+)>.*</\\1>", "<html>hh</html>")
if match_obj:
print(match_obj.group())
else:
print("匹配失败")
运行结果:
<html>hh</div>
<html>hh</html>
需求:匹配出www.baidu.cn
match_obj = re.match("<([a-zA-Z1-6]+)><([a-zA-Z1-6]+)>.*</\\2></\\1>", "<html><h1>www.baidu.cn</h1></html>")
if match_obj:
print(match_obj.group())
else:
print("匹配失败")
运行结果:
<html><h1>www.itcast.cn</h1></html>
示例4:(?P)(?P=name)
需求:匹配出www.itcast.cn
match_obj = re.match("<(?P<name1>[a-zA-Z1-6]+)><(?P<name2>[a-zA-Z1-6]+)>.*</(?P=name2)></(?P=name1)>", "<html><h1>www.itcast.cn</h1></html>")
if match_obj:
print(match_obj.group())
else:
print("匹配失败")
运行结果:
<html><h1>www.itcast.cn</h1></html>
边栏推荐
- 西瓜书《机器学习》阅读笔记之第一章绪论
- /dev/loop1占用100%问题
- Selenium reports an error: session not created: this version of chromedriver only supports chrome version 81
- flutter —— 布局原理与约束
- 《剑指Offer》 链表反转
- lua学习笔记
- 【剑指offer】面试题41:数据流中的中位数——大、小堆实现
- Network equipment hard core technology insider router Chapter 14 from deer by device to router (middle)
- USB interface electromagnetic compatibility (EMC) solution
- 【剑指offer】面试题54:二叉搜索树的第k大节点
猜你喜欢

学习Parquet文件格式

【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN

【剑指offer】面试题46:把数字翻译成字符串——动态规划

Leetcode 240. search two-dimensional matrix II medium

md 中超链接的解析问题:解析`this.$set()`,`$`前要加空格或转义符 `\`
![[daily question 1] 558. Intersection of quadtrees](/img/96/16ec3031161a2efdb4ac69b882a681.png)
[daily question 1] 558. Intersection of quadtrees

Dan bin Investment Summit: on the importance of asset management!

使用Lombok导致打印的tostring中缺少父类的属性

适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制

Photoelectric isolation circuit design scheme (six photoelectric isolation circuit diagrams based on optocoupler and ad210an)
随机推荐
Problem solving in magic tower project
Overview of wechat public platform development
QT (XIII) qchart drawing line chart
js使用一元运算符简化字符串转数字
【剑指offer】面试题41:数据流中的中位数——大、小堆实现
【剑指offer】面试题52:两个链表的第一个公共节点——栈、哈希表、双指针
Database: use the where statement to retrieve (header song)
Network equipment hard core technology insider router Chapter 21 reconfigurable router
Sword finger offer merges two sorted linked lists
Jump to the specified position when video continues playing
Two stage submission and three stage submission
Network equipment hard core technology insider router Chapter 5 tompkinson roaming the network world (Part 1)
STL value string learning
华为鸿蒙模拟器去除顶部导航栏方法
一文读懂鼠标滚轮事件(wheelEvent)
【剑指offer】面试题54:二叉搜索树的第k大节点
Discussion on STM32 power down reset PDR
Tools - common methods of markdown editor
npm install错误 unable to access
After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues