Python 正则表达式1 函数基础

ops/2024/9/25 2:46:47/

正则表达式主要函数

注:表达式正则表达式字符串指待处理文本。

函数名称概要
re.match()匹配检查字符串是否符合表达式,返回Match对象
re.search()搜索搜索字符串是否包含表达式,返回Match对象
re.findall()查询查询字符串所有符合表达式,返回列表对象
re.split()分割将字符串按表达式分割多项,返回列表对象
re.sub()替换替换字符串所有符合表达式,返文本对象
re.finditer()查询查询字符串所有符合表达式,返回迭代对象

补充说明:

python">import re
text = "Hello world, this is a sample text with words of different lengths."# 匹配,检查text是否符合pattern的规则
pattern = r'Hello'
re.match(pattern, text) # 整个文本匹配成功,返回包含"Hello"的匹配对象
pattern = r'world'
re.match(pattern, text) # 整个文本匹配失败,返回None# 搜索,搜索text里面符合pattern的子表达式
pattern = r'world'
re.search(pattern, text) # 表达式搜索成功,返回包含"world"的匹配对象
pattern = r'world1234'
re.search(pattern, text) # 表达式搜索失败,返回None

re.findall()返回字符串列表,无位置信息;re.finditer()相当于返回匹配对象列表,有位置信息。根据是否关注子串位置,选择合适函数。

正则表达式主要用法

为了方便理解,代码里面使用了最简单的正则表达式规则,实际应用会使用更复杂的表达式。

Match

在 Python 的 re 模块中,当成功匹配一个正则表达式时,通常会返回一个 Match 对象。Match 对象包含了关于该次匹配的信息,以及可以对匹配结果进行操作的方法。以下是一些 Match 对象的主要属性和方法:

group():返回匹配到的字符串。
start():返回匹配到的子串在原始字符串中的起始索引。
end():返回匹配到的子串在原始字符串中的结束索引。

匹配或搜索

python">import re
pattern = r'world'
text = "Hello world, this is a sample text with words of different lengths."
# 使用 match() 函数
match_result = re.match(pattern, text)
if match_result:print("match() found:", match_result.group())
else:print("match() did not find a match")
# 使用 search() 函数
search_result = re.search(pattern, text)
if search_result:print("search() found:", search_result.group())
else:print("search() did not find a match")

输出

match() did not find a match
search() found: world

查询或分割

python">import re
# 定义正则表达式模式
pattern = r'a'
# 待匹配的字符串
text = "Hello world, this is a sample text with words of different lengths."
# 使用 findall() 函数进行匹配
find_result = re.findall(pattern, text)
# 输出查询结果
print("Matches found:", find_result)
# 使用 split() 函数进行分割
split_result = re.split(pattern, text)
# 输出分割结果
print("Split result:", split_result)

输出

Matches found: ['a', 'a']
Split result: ['Hello world, this is ', ' s', 'mple text with words of different lengths.']

替换

python">import re
# 定义正则表达式模式
pattern = r'a'
# 待替换的字符串
text = "There are 10 apples and 20 oranges."
# 使用 sub() 函数进行替换
replacement = "aaa"
sub_result = re.sub(pattern, replacement, text)
# 输出替换结果
print("Substitution result:", sub_result)

输出

Substitution result: There aaare 10 aaapples aaand 20 oraaanges.

含位置查询

python">import re
# 定义正则表达式模式
pattern = r'a'
# 待匹配的字符串
text = "Hello world, this is a sample text with words of different lengths."
# 使用 finditer() 函数进行匹配
matches = re.finditer(pattern, text)
print(matches)
# 遍历匹配结果并输出位置信息
for match in matches:start_index = match.start()  # 匹配子串的起始索引end_index = match.end()      # 匹配子串的结束索引print(f"Match '{match.group()}' found at positions {start_index} to {end_index}")

输出

<callable_iterator object at 0x000001965C21FF40>
Match 'a' found at positions 21 to 22
Match 'a' found at positions 24 to 25

http://www.ppmy.cn/ops/31731.html

相关文章

java里的i/o流

在Java中&#xff0c;I/O&#xff08;输入/输出&#xff09;流是用于处理输入和输出操作的抽象概念。Java的I/O库提供了许多类和方法&#xff0c;用于从各种来源&#xff08;如文件、网络、内存等&#xff09;读取数据&#xff08;输入流&#xff09;&#xff0c;以及将数据写入…

Java-IO-ByteArray流的使用

在下面的Main类中定义一个实例方法&#xff0c;方法的声明如下&#xff1a;public ByteArrayOutputStream getByteArrayOutputStream(ByteArrayInputStream bais){ } 在上述的方法中实现从ByteArrayInputStream bais中读取byte数据并写入到输出流ByteArrayOutputStream&#xf…

【STM32】F405/407的模块总览图,记录查看

从STM32F405/407数据手册中提取&#xff0c;方便以后查看。主要是什么外设连接在什么总线上&#xff0c;时钟频率是多少。 TIM2、3、4、5、12、13、14在APB1上&#xff0c;最大频率84M TIM1、8、9、10、11在APB2上&#xff0c;最大频率168M

jupyter notebook导出pdf文件显示不了中文

找到文件index.tex.j2&#xff0c;我的在 C:\Users\Administrator\miniconda3\envs\opencv2\share\jupyter\nbconvert\templates\latex 我安装miniconda3并配置opencv2所需要的环境, 配置前 最后&#xff1a;用文本编辑器打开&#xff0c;修改图中article为ctexart&#xf…

美团面试(一面)

前言 给位小伙伴好&#xff0c;这里呢&#xff0c;分享一下最近一次美团的面试的面经&#xff0c;自己把面试的大多数内容通过博客的形式记录了下来&#xff0c;希望对各位有所帮助哦~ 一、项目篇 1、**对于自己的点餐小程序数据库表是怎么设计的 2、对于多个人下订单的问题…

ubuntu搭建jupyter_notebook服务器

环境&#xff1a;ubuntu 22.04 目录 环境&#xff1a;ubuntu 22.04 一、创建一个anaconda用户 创建用户condaUser 为用户condaUser设置密码 开放opt文件夹的权限 登录condaUser用户 二、安装anaconda 下载anaconda 安装anaconda 三、添加环境变量 四、anaconda换源 …

快速入门Pandas和NumPy数据分析

大家好&#xff0c;从商业智能到科学研究&#xff0c;数据分析在许多领域中都是一项重要技能。Python因其可读性强和强大的库生态系统而成为最受欢迎的数据分析语言之一&#xff0c;Pandas和NumPy是重要的基础工具&#xff0c;适用于任何想要分析和解释数据的人。本文将探讨如何…

数据库(MySQL)基础:事务

一、事务简介 事务 是一组操作的集合&#xff0c;它是一个不可分割的工作单位&#xff0c;事务会把所有的操作作为一个整体一起向系统提交或撤销操作请求&#xff0c;即这些操作要么同时成功&#xff0c;要么同时失败。 默认MySQL的事务是自动提交的&#xff0c;即&#xff0…