Python 正则表达式1 函数基础

发布于:2024-05-03 ⋅ 阅读:(207) ⋅ 点赞:(0)

正则表达式主要函数

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

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

补充说明:

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():返回匹配到的子串在原始字符串中的结束索引。

匹配或搜索

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

查询或分割

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.']

替换

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.

含位置查询

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

网站公告

今日签到

点亮在社区的每一天
去签到