Python 正则表达式详解
引言
正则表达式(Regular Expression, 简称 regex 或 regexp)是一种强大的文本处理工具,可以用来匹配、查找、替换和分割字符串。在 Python 中,re 模块提供了对正则表达式的全面支持。本文将详细介绍正则表达式的基本概念、如何使用正则表达式进行字符串匹配、替换和分割。
1. 正则表达式基本概念
正则表达式是一种描述字符串模式的语法。通过定义特定的模式,我们可以轻松地识别和操作符合该模式的字符串。正则表达式广泛应用于各种编程语言中,包括 Python。
主要用途:
- 匹配:检查一个字符串是否符合某个模式。
- 查找:在一个较大的字符串中查找符合某个模式的所有子串。
- 替换:将符合某个模式的子串替换为新的内容。
- 分割:根据某个模式将字符串分割成多个部分。
常用元字符:
.:匹配除换行符以外的任意字符。^:匹配字符串的开始位置。$:匹配字符串的结束位置。*:匹配前面的子表达式零次或多次。+:匹配前面的子表达式一次或多次。?:匹配前面的子表达式零次或一次。{n}:匹配前面的子表达式恰好 n 次。{n,}:匹配前面的子表达式至少 n 次。{n,m}:匹配前面的子表达式至少 n 次,但不超过 m 次。[]:定义一个字符类,匹配其中的任何一个字符。():分组,用于提取子串或应用量词到整个组。
2. 匹配字符串
使用 re 模块中的 search 和 match 函数可以实现字符串的匹配。
示例代码:
import re
# 示例字符串
text = "Hello, my email is example@example.com and my phone number is 123-456-7890."
# 定义正则表达式
email_pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
phone_pattern = r"\d{3}-\d{3}-\d{4}"
# 使用 search 函数匹配第一个符合条件的子串
email_match = re.search(email_pattern, text)
if email_match:
print("找到的电子邮件地址:", email_match.group(0)) # 输出: example@example.com
# 使用 match 函数从字符串的开头开始匹配
phone_match = re.match(phone_pattern, text)
if not phone_match:
print("没有从字符串开头找到电话号码")
# 使用 findall 函数查找所有符合条件的子串
all_emails = re.findall(email_pattern, text)
print("找到的所有电子邮件地址:", all_emails) # 输出: ['example@example.com']
3. 替换字符串
使用 re.sub 函数可以实现字符串的替换。
示例代码:
import re
# 示例字符串
text = "Hello, my email is example@example.com and my phone number is 123-456-7890."
# 定义正则表达式
email_pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
phone_pattern = r"\d{3}-\d{3}-\d{4}"
# 使用 sub 函数替换电子邮件地址
new_text = re.sub(email_pattern, "[REDACTED]", text)
print("替换后的文本:", new_text) # 输出: Hello, my email is [REDACTED] and my phone number is 123-456-7890.
# 使用 sub 函数替换电话号码,并返回替换次数
new_text, count = re.subn(phone_pattern, "[REDACTED]", new_text)
print("替换后的文本:", new_text) # 输出: Hello, my email is [REDACTED] and my phone number is [REDACTED].
print("替换次数:", count) # 输出: 1
4. 使用正则表达式分割字符串
使用 re.split 函数可以根据正则表达式将字符串分割成多个部分。
示例代码:
import re
# 示例字符串
text = "apple,banana,orange;grape:melon"
# 定义正则表达式
delimiter_pattern = r"[,;:]"
# 使用 split 函数分割字符串
parts = re.split(delimiter_pattern, text)
print("分割后的部分:", parts) # 输出: ['apple', 'banana', 'orange', 'grape', 'melon']
# 使用 maxsplit 参数限制分割次数
limited_parts = re.split(delimiter_pattern, text, maxsplit=2)
print("限制分割次数后的部分:", limited_parts) # 输出: ['apple', 'banana', 'orange;grape:melon']
结论
本文详细介绍了 Python 中正则表达式的使用方法,包括如何匹配、替换和分割字符串。通过掌握这些技巧,你可以更高效地处理文本数据。正则表达式虽然强大,但有时也较为复杂,因此建议多加练习以熟练掌握其用法。

浙公网安备 33010602011771号