当前位置:网站首页>[regular expression] matching grouping
[regular expression] matching grouping
2022-07-27 15:48:00 【Grapefruit tree CC】
1. Matching grouping related regular expressions
| Code | function |
|---|---|
| | | Match any expression left or right |
| (ab) | Use the characters in brackets as a group |
\num | Reference group num Matching string |
(?P) | Group aliases |
| (?P=name) | Quote alias as name Group matched strings |
Example 1:|
demand : In the list [“apple”, “banana”, “orange”, “pear”], matching apple and pear
import re
# Fruit list
fruit_list = ["apple", "banana", "orange", "pear"]
# Traversal data
for value in fruit_list:
# | Match any expression left or right
match_obj = re.match("apple|pear", value)
if match_obj:
print("%s It's what I want " % match_obj.group())
else:
print("%s It's not what I want " % value)
Execution results :
apple It's what I want
banana It's not what I want
orange It's not what I want
pear It's what I want
Example 2:( )
demand : Match out 163、126、qq Wait for email
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())
# Get group data
print(match_obj.group(1))
else:
print(" Matching failure ")
Execution results :
[email protected]
163
demand : matching qq:10567 Data like this , extracted qq Text and qq number
import re
match_obj = re.match("(qq):([1-9]\d{4,10})", "qq:10567")
if match_obj:
print(match_obj.group())
# grouping : The default is 1 A group , Multiple groups add... From left to right 1
print(match_obj.group(1))
# Extract the second packet data
print(match_obj.group(2))
else:
print(" Matching failure ")
Execution results :
qq
10567
Example 3:\num
demand : Match out 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(" Matching failure ")
match_obj = re.match("<([a-zA-Z1-6]+)>.*</\\1>", "<html>hh</html>")
if match_obj:
print(match_obj.group())
else:
print(" Matching failure ")
Running results :
<html>hh</div>
<html>hh</html>
demand : Match out 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(" Matching failure ")
Running results :
<html><h1>www.itcast.cn</h1></html>
Example 4:(?P)(?P=name)
demand : Match out 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(" Matching failure ")
Running results :
<html><h1>www.itcast.cn</h1></html>
边栏推荐
- 使用Prometheus监控Spark任务
- 【云享读书会第13期】视频文件的编码格式
- 传美国政府将向部分美企发放对华为销售许可证!
- Analysis of spark task scheduling exceptions
- 设置提示框位置随鼠标移动,并解决提示框显示不全的问题
- synchronized和ReentrantLock的区别
- “router-link”各种属性解释
- Spark Bucket Table Join
- Huawei's general card identification function enables multiple card bindings with one key
- 【剑指offer】面试题56-Ⅰ:数组中数字出现的次数Ⅰ
猜你喜欢
随机推荐
网络原理(1)——基础原理概述
C language: custom type
First understanding of structure
Spark动态资源分配的资源释放过程及BlockManager清理过程
Catalog component design and custom extended catalog implementation in spark3
Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers
Learn parquet file format
UDP message structure and precautions
Summer Challenge harmonyos realizes a hand-painted board
网络设备硬核技术内幕 路由器篇 22
初识结构体
扩展Log4j支持日志文件根据时间分割文件和过期文件自动删除功能
$router.back(-1)
C language: Sanzi game
聊聊面试必问的索引
【剑指offer】面试题49:丑数
实体类(VO,DO,DTO)的划分
【剑指offer】面试题50:第一个只出现一次的字符——哈希表查找
The method of exchanging two numbers in C language
C语言中交换两数的方法








