引言
正则表达式(Regular Expression,简称 Regex)是Python中一种强大的文本处理工具,广泛应用于字符串搜索、替换、验证和提取等任务。掌握正则表达式,可以帮助开发者更高效地处理文本数据,解决各种文本处理难题。
正则表达式基础
1. 正则表达式语法
正则表达式由普通字符和特殊字符(元字符)组成。以下是一些常用的元字符及其含义:
.
:匹配除换行符以外的任意单个字符。*
:匹配前面的子表达式零次或多次。+
:匹配前面的子表达式一次或多次。?
:匹配前面的子表达式零次或一次。^
:匹配输入字符串的开始位置。$
:匹配输入字符串的结束位置。[...]
:匹配括号内的任意一个字符(字符类)。{n}
:匹配前面的子表达式恰好n次。{n,}
:匹配前面的子表达式至少n次。{n,m}
:匹配前面的子表达式至少n次,但不超过m次。
2. Python正则表达式模块
Python中的正则表达式功能主要由 re
模块提供。以下是 re
模块中的一些常用函数:
re.match(pattern, string)
:从字符串的开始位置匹配正则表达式。re.search(pattern, string)
:搜索整个字符串,返回第一个匹配的结果。re.findall(pattern, string)
:搜索整个字符串,返回所有匹配的结果列表。re.sub(pattern, replacement, string)
:替换字符串中所有匹配的子串。
高级技巧
1. 分组与引用
正则表达式中的括号用于创建分组,可以使用 ()
进行分组。分组后,可以通过 \1
、\2
等引用分组匹配的内容。
import re
text = "The rain in Spain falls mainly in the plain."
pattern = r"(\w+) in (\w+) falls"
matches = re.findall(pattern, text)
for match in matches:
print(match)
2. 贪婪匹配与非贪婪匹配
贪婪匹配会匹配尽可能多的字符,而非贪婪匹配会匹配尽可能少的字符。可以通过在量词后面添加 ?
来实现非贪婪匹配。
import re
text = "I have 3 apples and 2 oranges."
pattern = r"(\d+) apples and (\d+) oranges"
matches = re.findall(pattern, text)
for match in matches:
print(match)
3. 编译正则表达式
在处理大量数据或需要多次使用同一正则表达式时,使用 re.compile()
编译正则表达式可以提高效率。
import re
pattern = re.compile(r"(\d+) apples and (\d+) oranges")
text = "I have 3 apples and 2 oranges."
matches = pattern.findall(text)
for match in matches:
print(match)
实战案例
以下是一些使用正则表达式解决实际问题的案例:
1. 验证邮箱地址
import re
email = "example@example.com"
pattern = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
if re.match(pattern, email):
print("Valid email address")
else:
print("Invalid email address")
2. 提取电话号码
import re
text = "My phone number is 123-456-7890."
pattern = r"\d{3}-\d{3}-\d{4}"
phone_numbers = re.findall(pattern, text)
for number in phone_numbers:
print(number)
3. 替换敏感词
import re
text = "This is a bad word: badword"
pattern = r"badword"
replaced_text = re.sub(pattern, "****", text)
print(replaced_text)
总结
掌握Python正则表达式,可以帮助开发者更高效地处理文本数据,解决各种文本处理难题。通过学习正则表达式的语法、高级技巧和实战案例,可以轻松应对各种文本处理任务。