python语言 -- 正则分组处理字符串整理

devtools/2024/10/21 3:35:58/

Python 正则表达式匹配字符串以及分组用法

导入正则表达式模块

python">import re

基本匹配

使用 re.match()re.search()re.findall() 方法进行基本的字符串匹配。

  • re.match() 从字符串的开头进行匹配。
  • re.search() 在字符串的任意位置进行匹配。
  • re.findall() 返回所有匹配的子字符串。

示例

python">pattern = r'\d+'  # 匹配一个或多个数字text = "There are 123 apples and 456 oranges."# 从字符串开头进行匹配
match = re.match(pattern, text)
if match:print("Match found:", match.group())
else:print("No match at the beginning of the string.")# 在字符串任意位置进行匹配
search = re.search(pattern, text)
if search:print("Search found:", search.group())# 返回所有匹配的子字符串
findall = re.findall(pattern, text)
print("Findall found:", findall)

分组匹配

使用圆括号 () 在正则表达式中定义分组。分组可以提取匹配的子字符串。

示例

python">pattern = r'(\d+) apples and (\d+) oranges'text = "There are 123 apples and 456 oranges."match = re.search(pattern, text)
if match:print("Group 1:", match.group(1))print("Group 2:", match.group(2))

命名分组

可以使用 (?P<name>...) 语法为分组命名。

示例

python">pattern = r'(?P<apples>\d+) apples and (?P<oranges>\d+) oranges'text = "There are 123 apples and 456 oranges."match = re.search(pattern, text)
if match:print("Apples:", match.group('apples'))print("Oranges:", match.group('oranges'))

分组捕获所有匹配

使用 re.finditer() 返回一个迭代器,捕获所有匹配并可以访问每个匹配的分组。

示例

python">pattern = r'(\d+)'text = "There are 123 apples and 456 oranges."matches = re.finditer(pattern, text)
for match in matches:print("Match found:", match.group())

替换匹配的子字符串

使用 re.sub() 方法替换匹配的子字符串。

示例

python">pattern = r'(\d+) apples'text = "There are 123 apples and 456 apples."# 将所有匹配的 'apples' 替换为 'pears'
result = re.sub(pattern, r'\1 pears', text)
print("Substitution result:", result)

复杂示例

组合使用分组和替换来处理更复杂的文本。

示例

python">pattern = r'(\d+)\s+(apples|oranges)'text = "There are 123 apples and 456 oranges."def repl(match):quantity = int(match.group(1))fruit = match.group(2)return f'{quantity * 2} {fruit}'result = re.sub(pattern, repl, text)
print("Complex substitution result:", result)

http://www.ppmy.cn/devtools/90251.html

相关文章

七天打造一套量化交易系统:Day8-阶段性总结、未完待续...

七天打造一套量化交易系统&#xff1a;Day8-阶段性总结、未完待续... 阅读数据分析私信情况汇总如何收费代写策略功能拓展商务合作如何联系我 下一阶段规划 从 2024-07-18 准备进行【七天打造一套量化交易系统】系列分享&#xff0c;到昨天&#xff08;2024-08-04&#xff09;&…

计算机网络总结

1.TCP/IP 网络分层模型 第一层叫“链接层”&#xff08;link layer&#xff09;&#xff0c;负责在以太网、WiFi 这样的底层网络上发送原始数据包&#xff0c;工作在网卡这个层次上&#xff0c;使用 MAC 地址来标记网络上的设备。第二层叫“网际层”&#xff08;internet laye…

TCP和UDP的区别?

&#xff08;1&#xff09;关于连接&#xff1a; tcp是面向连接的传输层协议&#xff0c;传输数据之前需要建立连接&#xff1b; udp也是传输层协议&#xff0c;不需要建立连接&#xff0c;可以直接传输数据。 &#xff08;2&#xff09;服务对象&#xff1a; tcp是一对一的…

go语言 MVC模式web开发框架

Go语言中有多个流行的MVC模式的Web开发框架&#xff0c;MVC模式&#xff08;Model-View-Controller&#xff09;是Web开发中的一种常见架构模式&#xff0c;能够将应用程序的不同部分分离开来&#xff0c;从而更好地组织代码和提升可维护性。以下是几个流行的Go语言MVC框架&…

(leetcode学习)46. 全排列

给定一个不含重复数字的数组 nums &#xff0c;返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。 示例 1&#xff1a; 输入&#xff1a;nums [1,2,3] 输出&#xff1a;[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]示例 2&#xff1a; 输入&#xff1a;nums [0…

【Ren‘py】视觉小说游戏开发引擎 | 个人开发 | 基础 | 语法 | 自用 by.Akaxi

renpy中文官方网址&#xff1a;https://www.renpy.cn/ -------------------------------------------------------------------- 一、Renpy启动器 Renpy提供了一个launcher&#xff0c;类似于开发者平台&#xff0c;开发者集中环境&#xff0c;需要在笔记本下载&#xff0c;参…

C# winform 串口读取字节流,MB级别字节流

一、串口读取字节流 在 C# 中使用 Windows Forms (WinForms) 应用程序进行串口通信时&#xff0c;通常会使用 System.IO.Ports 命名空间中的 SerialPort 类。以下是一个简单的示例&#xff0c;展示了如何设置一个串口并读取字节流。 步骤 1: 添加引用 确保你的项目中已经包含…

CSS:图片间空白间距问题的解决方案

一、问题描述 今天有小伙伴遇到多张图片排版显示时中间存在空白间隙&#xff0c;问如何处理&#xff1a; <div> <img width"100%" src"https:/xxx.png" id"1747098" style"max-width:100%;"><img width"100%&qu…