当前位置:网站首页>[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>
边栏推荐
- [TensorBoard] OSError: [Errno 22] Invalid argument处理
- 【云享读书会第13期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
- 学习Parquet文件格式
- Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`
- md 中超链接的解析问题:解析`this.$set()`,`$`前要加空格或转义符 `\`
- 一文读懂鼠标滚轮事件(wheelEvent)
- 低代码是开发的未来吗?浅谈低代码平台
- 使用Prometheus监控Spark任务
- C:浅谈函数
- The method of exchanging two numbers in C language
猜你喜欢

C language: minesweeping games

网络原理(1)——基础原理概述

C language: function stack frame

Network principle (1) - overview of basic principles

Push down of spark filter operator on parquet file

Causes and solutions of deadlock in threads

The difference between synchronized and reentrantlock

Spark Bucket Table Join

C语言:自定义类型

Is the array name the address of the first element?
随机推荐
Go language learning notes (1)
Spark 3.0 测试与使用
【剑指offer】面试题54:二叉搜索树的第k大节点
Extended log4j supports the automatic deletion of log files according to time division and expired files
股票开户佣金优惠,炒股开户哪家证券公司好网上开户安全吗
聊聊ThreadLocal
修改 Spark 支持远程访问OSS文件
js使用for in和for of来简化普通for循环
Network principle (1) - overview of basic principles
QT (five) meta object properties
扩展Log4j支持日志文件根据时间分割文件和过期文件自动删除功能
C language: dynamic memory function
设置提示框位置随鼠标移动,并解决提示框显示不全的问题
Modify spark to support remote access to OSS files
【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
Network principle (2) -- network development
【剑指offer】面试题52:两个链表的第一个公共节点——栈、哈希表、双指针
Fluent -- layout principle and constraints
后台返回来的是这种数据,是什么格式啊
实现自定义Spark优化规则