Python 正则表达式(RegEx)
Python 正则表达式(Regular Expressions,简称 RegEx)是用于模式匹配和字符串操作的强大工具。通过预定义的模式,Python 正则表达式可以快速地搜索、匹配和操作文本。无论你是验证用户输入、解析数据还是从大量文本文件中提取信息,Python 正则表达式都能显著提升你的编程能力。
本文将帮助你熟练掌握 Python 正则表达式,介绍其基础知识、工作原理,并提供实际应用示例。通过本文的学习,你将具备在各种实际应用中使用正则表达式的技能,提高编程的有效性和效率。
Python 中的正则表达式模块 (re
)
Python 的 re
模块提供了一组用于处理正则表达式的函数。它使你能够使用特定的模式来搜索、匹配和操作文本。以下是 re
模块的主要概念和功能:
1. 导入模块
在使用正则表达式函数之前,需要导入 re
模块:
import re
2. 基本函数
search()
:在字符串中搜索匹配项,并返回一个匹配对象(如果找到)。
match = re.search(r'\d+', 'There are 123 apples')
print(match.group()) # 输出: 123
match()
:检查字符串的开头是否与模式匹配。
match = re.match(r'Hello', 'Hello, world!')
print(match.group()) # 输出: Hello
findall()
:查找字符串中所有匹配项,并返回一个匹配项列表。
matches = re.findall(r'\d+', '123 apples and 456 oranges')
print(matches) # 输出: ['123', '456']
sub()
:将匹配项替换为指定的字符串。
result = re.sub(r'apples', 'bananas', 'I like apples')
print(result) # 输出: I like bananas
3. 特殊字符
.
(点号):匹配除换行符以外的任何单个字符。^
(脱字符):匹配字符串的开始。$
(美元符号):匹配字符串的结束。[]
(方括号):匹配方括号内的任何一个字符。\
(反斜杠):转义特殊字符或表示特定序列。4. 特殊序列
\d
:匹配任何数字。\D
:匹配任何非数字字符。\s
:匹配任何空白字符(如空格、制表符、换行符)。\S
:匹配任何非空白字符。\w
:匹配任何字母数字字符(包括下划线)。\W
:匹配任何非字母数字字符。5. 量词
*
:匹配前面的模式零次或多次。+
:匹配前面的模式一次或多次。?
:匹配前面的模式零次或一次。{n}
:精确匹配前面的模式 n 次。{n,}
:匹配前面的模式至少 n 次。{n,m}
:匹配前面的模式至少 n 次,最多 m 次。6. 编译模式
为了提高性能,特别是对于多次使用的模式,可以使用 re.compile()
编译正则表达式模式。
pattern = re.compile(r'\d+')
matches = pattern.findall('123 apples and 456 oranges')
print(matches) # 输出: ['123', '456']
实际应用示例
验证用户输入
def validate_email(email):
pattern = re.compile(r'^[\w\.-]+@[\w\.-]+\.\w+$')
return bool(pattern.match(email))
print(validate_email('test@example.com')) # 输出: True
print(validate_email('invalid-email')) # 输出: False
解析日志文件
log_line = '2023-10-01 12:34:56 INFO User logged in'
pattern = re.compile(r'(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}) (.+)')
match = pattern.search(log_line)
if match:
date, time, message = match.groups()
print(f'Date: {date}, Time: {time}, Message: {message}')
# 输出: Date: 2023-10-01, Time: 12:34:56, Message: INFO User logged in
替换敏感信息
text = 'My credit card number is 1234-5678-9012-3456'
pattern = re.compile(r'\d{4}-\d{4}-\d{4}-\d{4}')
redacted_text = pattern.sub('XXXX-XXXX-XXXX-XXXX', text)
print(redacted_text) # 输出: My credit card number is XXXX-XXXX-XXXX-XXXX
如何在 Python 中使用正则表达式(RegEx)
要在 Python 中搜索、匹配和编辑字符串,你需要导入 re
模块并使用其提供的函数来创建正则表达式(RegEx)。以下是一些使用正则表达式的指令和示例。
1. 导入 re
模块
首先,需要导入 re
模块:
import re
2. 使用 search()
函数
search()
函数在字符串中搜索匹配项,并返回一个匹配对象(如果找到)。
import re
text = "The price is 123 dollars"
match = re.search(r'\d+', text)
if match:
print("Found a match:", match.group()) # 输出: Found a match: 123
3. 使用 match()
函数
match()
函数检查字符串的开头是否与模式匹配。
import re
text = "Hello, world!"
match = re.match(r'Hello', text)
if match:
print("Found a match:", match.group()) # 输出: Found a match: Hello
4. 使用 findall()
函数
findall()
函数查找字符串中所有匹配项,并返回一个匹配项列表。
import re
text = "123 apples and 456 oranges"
matches = re.findall(r'\d+', text)
print("All matches:", matches) # 输出: All matches: ['123', '456']
5. 使用 sub()
函数
sub()
函数将匹配项替换为指定的字符串。
import re
text = "I like apples"
result = re.sub(r'apples', 'bananas', text)
print("Replaced text:", result) # 输出: Replaced text: I like bananas
6. 使用 compile()
函数
compile()
函数将正则表达式模式编译为正则表达式对象,以便重复使用。
import re
pattern = re.compile(r'\d+')
text = "123 apples and 456 oranges"
matches = pattern.findall(text)
print("All matches:", matches) # 输出: All matches: ['123', '456']
实际应用示例
以下是一个使用正则表达式从文本中提取电子邮件地址的实用示例。
import re
text = """
Contact us at support@example.com for more information.
You can also reach out to sales@example.com or marketing@example.net.
"""
# 定义电子邮件地址的正则表达式模式
email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
# 使用 findall() 提取所有电子邮件地址
email_addresses = re.findall(email_pattern, text)
# 打印提取的电子邮件地址
print("Extracted email addresses:", email_addresses)
# 输出: Extracted email addresses: ['support@example.com', 'sales@example.com', 'marketing@example.net']
正则表达式函数(RegEx Functions)
1. findall()
import re
text = "123 apples, 456 oranges, and 789 bananas"
matches = re.findall(r'\d+', text)
print(matches) # 输出: ['123', '456', '789']
2. search()
import re
text = "Hello, world!"
match = re.search(r'world', text)
if match:
print("Found:", match.group()) # 输出: Found: world
3. split()
import re
text = "one, two, three; four"
parts = re.split(r'[,;]', text)
print(parts) # 输出: ['one', ' two', ' three', ' four']
4. sub()
import re
text = "I like apples"
result = re.sub(r'apples', 'bananas', text)
print(result) # 输出: I like bananas
5. compile()
import re
pattern = re.compile(r'\d+')
text = "123 apples and 456 oranges"
matches = pattern.findall(text)
print(matches) # 输出: ['123', '456']
6. escape()
import re
text = "example.com?query=value"
escaped_text = re.escape(text)
print(escaped_text) # 输出: example\.com\?query\=value
7. fullmatch()
import re
pattern = r'Hello, world!'
text = 'Hello, world!'
match = re.fullmatch(pattern, text)
if match:
print("Exact match!") # 输出: Exact match!
元字符(MetaCharacters)
元字符是正则表达式中具有特殊意义的字符。以下是一些常见的元字符及其解释和示例:
1. []
import re
text = "bat, cat, hat"
matches = re.findall(r'[bch]at', text)
print(matches) # 输出: ['bat', 'cat', 'hat']
2. \
\d
表示数字)。import re
text = "This is a test. 123."
matches = re.findall(r'\d+', text)
print(matches) # 输出: ['123']
3. .
import re
text = "cat, cot, cut"
matches = re.findall(r'c.t', text)
print(matches) # 输出: ['cat', 'cot', 'cut']
4. ^
import re
text = "Hello, world!"
match = re.search(r'^Hello', text)
if match:
print("Found:", match.group()) # 输出: Found: Hello
5. $
import re
text = "Welcome to Python"
match = re.search(r'Python$', text)
if match:
print("Found:", match.group()) # 输出: Found: Python
6. *
import re
text = "ac, abc, abbc"
matches = re.findall(r'ab*c', text)
print(matches) # 输出: ['ac', 'abc', 'abbc']
7. +
import re
text = "ac, abc, abbc"
matches = re.findall(r'ab+c', text)
print(matches) # 输出: ['abc', 'abbc']
8. ?
import re
text = "color, colour"
matches = re.findall(r'colou?r', text)
print(matches) # 输出: ['color', 'colour']
9. |
import re
text = "cat, bat, rat"
matches = re.findall(r'cat|rat', text)
print(matches) # 输出: ['cat', 'rat']
特殊序列(Special Sequences)
特殊序列是正则表达式中具有特定含义的转义序列。以下是一些常见的特殊序列及其解释和示例:
1. \A
^
,但更严格(仅在字符串开头有效)。import re
text = "Hello world"
match = re.search(r'\AHello', text)
if match:
print("Found:", match.group()) # 输出: Found: Hello
2. \b
import re
text = "Hello, world!"
matches = re.findall(r'\bworld\b', text)
print(matches) # 输出: ['world']
3. \B
import re
text = "Hello, world!"
matches = re.findall(r'\Bworld\B', text)
print(matches) # 输出: []
4. \d
[0-9]
。import re
text = "There are 123 apples"
matches = re.findall(r'\d+', text)
print(matches) # 输出: ['123']
5. \D
[^0-9]
。import re
text = "There are 123 apples"
matches = re.findall(r'\D+', text)
print(matches) # 输出: ['There are ', ' apples']
6. \s
[ \t\n\r\f\v]
。import re
text = "Hello world!"
matches = re.findall(r'\s', text)
print(matches) # 输出: [' ']
7. \S
[^ \t\n\r\f\v]
。import re
text = "Hello world!"
matches = re.findall(r'\S+', text)
print(matches) # 输出: ['Hello', 'world!']
8. \w
[a-zA-Z0-9_]
。import re
text = "Hello_world 123"
matches = re.findall(r'\w+', text)
print(matches) # 输出: ['Hello_world', '123']
9. \W
[^a-zA-Z0-9_]
。import re
text = "Hello world!"
matches = re.findall(r'\W+', text)
print(matches) # 输出: [' ', '!']
10. \Z
$
,但更严格(仅在字符串末尾有效)。import re
text = "Hello world"
match = re.search(r'world\Z', text)
if match:
print("Found:", match.group()) # 输出: Found: world
集合(Sets)
集合是正则表达式中用于指定一组字符的工具。以下是一些常见的集合及其解释和示例:
1. [arn]
import re
text = "apple, banana, orange"
matches = re.findall(r'[arn]', text)
print(matches) # 输出: ['a', 'a', 'a', 'n', 'a', 'a', 'a']
2. [a-n]
import re
text = "apple, banana, orange"
matches = re.findall(r'[a-n]', text)
print(matches) # 输出: ['a', 'l', 'e', 'a', 'a', 'n', 'a', 'a', 'e']
3. [^arn]
import re
text = "apple, banana, orange"
matches = re.findall(r'[^arn]', text)
print(matches) # 输出: ['p', 'p', 'l', 'e', 'b', ' ', 'g', 'e']
4. [0123]
import re
text = "1024, 123, 456"
matches = re.findall(r'[0123]', text)
print(matches) # 输出: ['1', '0', '2', '1', '2', '3']
5. [0-9]
\d
。import re
text = "1024, 123, 456"
matches = re.findall(r'[0-9]', text)
print(matches) # 输出: ['1', '0', '2', '4', '1', '2', '3', '4', '5', '6']
6. [0-5][0-9]
import re
text = "The time is 12:45 and 08:30."
matches = re.findall(r'[0-5][0-9]', text)
print(matches) # 输出: ['12', '45', '08', '30']
7. [a-zA-Z]
import re
text = "Hello, World!"
matches = re.findall(r'[a-zA-Z]', text)
print(matches) # 输出: ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
8. [+]
+
。import re
text = "Use + for addition"
matches = re.findall(r'[+]', text)
print(matches) # 输出: ['+']
常见问题解答
1. 如何检查字符串是否匹配正则表达式?
要检查字符串是否匹配正则表达式模式,可以使用 re
模块的 match()
、search()
或 fullmatch()
函数。
re.match()
:检查字符串的开头是否匹配正则表达式模式。re.search()
:扫描整个字符串,查找任何匹配模式的部分。re.fullmatch()
:确保整个字符串匹配正则表达式模式。每个函数在找到匹配项时返回一个匹配对象,如果没有找到匹配项则返回 None
。
示例:
import re
text = "Hello 123"
# 使用 re.search 查找数字
match = re.search(r'\d+', text)
if match:
print("Found:", match.group()) # 输出: Found: 123
else:
print("No match found")
2. 如何在 Python 中使用正则表达式搜索短语?
要在字符串中使用正则表达式搜索短语,可以使用 re
模块的 search()
函数。首先导入 re
模块,然后定义包含你要搜索的确切短语的正则表达式模式。re.search()
函数会扫描整个字符串,查找匹配模式的部分,如果找到短语,则返回一个匹配对象;否则返回 None
。
示例:
import re
text = "This is a sample text with hello world in it."
# 使用 re.search 搜索短语 "hello world"
match = re.search(r'hello world', text)
if match:
print("Found:", match.group()) # 输出: Found: hello world
else:
print("No match found")
3. 如何在 Python 中使用正则表达式替换文本文件中的内容?
要在文件中使用正则表达式替换文本,可以使用 re
模块的 sub()
函数。首先读取文件内容到一个字符串中,然后使用 re.sub()
定义要替换的模式和替换文本。执行替换后,将修改后的内容写回文件。
示例:
import re
# 读取文件内容
with open('example.txt', 'r') as file:
content = file.read()
# 使用正则表达式替换文本
updated_content = re.sub(r'foo', 'bar', content)
# 将修改后的内容写回文件
with open('example.txt', 'w') as file:
file.write(updated_content)
4. 如何在 Python 中使用正则表达式查找全名?
要在 Python 中使用正则表达式查找全名,可以创建一个匹配典型名字格式的模式。常见的全名模式由两个单词组成,每个单词以大写字母开头,后面跟着小写字母,并且可能包括中间名或首字母。可以使用 re
模块的 findall()
或 search()
函数来定位文本中的名字。
示例:
import re
text = "Contact John Doe or Jane Smith for more information."
# 定义全名的正则表达式模式
pattern = r'\b[A-Z][a-z]+\s[A-Z][a-z]+\b'
# 查找文本中的所有全名
full_names = re.findall(pattern, text)
print(full_names) # 输出: ['John Doe', 'Jane Smith']
总结
Python 正则表达式(RegEx)是一种强大且灵活的工具,用于模式匹配和字符串操作,广泛应用于从文本处理到数据验证的各种场景。熟练掌握 re
模块及其函数,如 match()
、sub()
、findall()
和 search()
,可以有效地处理复杂的文本处理任务。Python 正则表达式提供了足够的灵活性和效率,使得诸如文本替换、信息提取和模式识别等任务变得简单易行。
主要内容回顾
-
模式匹配:
match()
:检查字符串的开头是否匹配正则表达式模式。search()
:扫描整个字符串,查找任何匹配模式的部分。fullmatch()
:确保整个字符串匹配正则表达式模式。-
字符串操作:
findall()
:返回字符串中所有匹配模式的列表。sub()
:将字符串中匹配模式的部分替换为指定的替换字符串。split()
:按照模式出现的位置拆分字符串,并返回子字符串的列表。-
特殊序列和集合:
- 特殊序列:如
\d
(匹配数字)、\s
(匹配空白字符)、\w
(匹配字母数字字符)等,提供了一种方便的方式来匹配特定类型的字符。 - 集合:如
[arn]
(匹配 ‘a’、‘r’ 或 ‘n’)、[a-n]
(匹配 ‘a’ 到 ‘n’ 范围内的字符)、[^arn]
(匹配不在 ‘a’、‘r’ 或 ‘n’ 中的字符)等,允许你定义更复杂的匹配规则。
实际应用
re.sub()
函数可以轻松地替换文本中的特定模式。re.findall()
或 re.search()
可以从文本中提取所需的信息。re.match()
或 re.fullmatch()
可以验证字符串是否符合特定的模式。作者:疯一样的码农