在Python中,正则表达式是一种强大的工具,用于处理字符串的搜索、替换和分割等操作。Python的re模块提供了丰富的函数来支持这些功能。下面将详细介绍如何使用正则表达式进行字符串匹配和替换,并给出具体的示例。

  1. 导入re模块
    首先,需要导入Python的re模块,该模块包含了所有与正则表达式相关的函数。

    import re

  2. 使用re.sub()函数进行字符串替换
    re.sub()函数用于替换字符串中所有匹配正则表达式的部分。其基本语法如下:

    re.sub(pattern, repl, string, count=0, flags=0)

pattern:正则表达式模式。
repl:替换后的字符串或替换函数。
string:要被处理的原始字符串。
count:可选参数,指定最多替换次数,默认为0,表示替换所有匹配项。
flags:可选参数,用于修改正则表达式的匹配方式,例如忽略大小写等。
示例1:

将字符串中的"java script"替换为"javascript"。

import re
 
text = "java script is awesome."
pattern = r"\bjava script\b"
repl = "javascript"
new_text = re.sub(pattern, repl, text)
print(new_text)  # 输出: javascript is awesome.

示例2:

将字符串中的所有数字替换为"**"。

import re
 
text = "1234 hello 5678 world"
pattern = r"\b\d{4}\b"
repl = "****"
new_text = re.sub(pattern, repl, text)
print(f'Original string: {text}')
print(f'Replaced string: {new_text}')
# 输出:
# Original string: 1234 hello 5678 world
# Replaced string: ****hello**** world
  1. 使用re.search()函数进行字符串匹配
    re.search()函数用于在字符串中搜索匹配正则表达式的部分。其基本语法如下:

    re.search(pattern, string, flags=0)

pattern:正则表达式模式。

string:要被处理的原始字符串。

flags:可选参数,用于修改正则表达式的匹配方式,例如忽略大小写等。

示例:

检查字符串中是否包含"World"。

import re
 
text = "Hello World"
pattern = r"World"
match = re.search(pattern, text)
if match:
    print("匹配成功")
    print(match.group())  # 输出: World
else:
    print("匹配失败")
  1. 使用re.match()函数进行字符串匹配
    re.match()函数用于从字符串的开头开始匹配正则表达式的部分。其基本语法如下:

    re.match(pattern, string, flags=0)

pattern:正则表达式模式。

string:要被处理的原始字符串。

flags:可选参数,用于修改正则表达式的匹配方式,例如忽略大小写等。

示例:

检查字符串是否以"Hello"开头。

import re
 
text = "Hello World"
pattern = r"Hello"
match = re.match(pattern, text)
if match:
    print("匹配成功")
    print(match.group())  # 输出: Hello
else:
    print("匹配失败")
  1. 使用正则表达式进行复杂的字符串替换
    有时需要根据匹配的内容动态地生成替换字符串,这时可以使用替换函数作为re.sub()的第二个参数。

示例:

将字符串中的所有数字替换为其两倍的值。

import re
 
text = "The numbers are 123 and 456."
pattern = r"\d+"
 
def double(match):
    num = int(match.group())
    return str(num * 2)
 
new_text = re.sub(pattern, double, text)
print(new_text)  # 输出: The numbers are 246 and 912.
  1. 使用正则表达式进行多模式替换
    有时需要对字符串进行多次替换,可以使用字典来定义替换规则,然后通过循环进行替换。

示例:

将字符串中的特定单词替换为其他单词。

import re
 
text = "apple banana cherry"
rep = {"apple": "orange", "banana": "grape"}
 
# 将字典中的键进行转义
rep = dict((re.escape(k), v) for k, v in rep.items())
 
# 创建正则表达式对象
pattern = re.compile("|".join(rep.keys()))
 
# 进行替换
new_text = pattern.sub(lambda m: rep[re.escape(m.group(0))], text)
print(new_text)  # 输出: orange grape cherry
  1. 使用正则表达式进行贪婪和非贪婪匹配
    贪婪匹配会尽可能多地匹配字符,而非贪婪匹配会尽可能少地匹配字符。可以通过在量词后面加上?来实现非贪婪匹配。

示例:

使用贪婪匹配和非贪婪匹配来提取字符串中的内容。

import re
text = "<title>Example</title> <body>Content</body>"
# 贪婪匹配
pattern_greedy = r"<title>(.*)</title>"
match_greedy = re.search(pattern_greedy, text)
if match_greedy:
    print("贪婪匹配结果:", match_greedy.group(1))  # 输出: Example</title> <body>Content
 
# 非贪婪匹配
pattern_non_greedy = r"<title>(.*?)</title>"
match_non_greedy = re.search(pattern_non_greedy, text)
if match_non_greedy:
    print("非贪婪匹配结果:", match_non_greedy.group(1))  # 输出: Example
  1. 使用正则表达式进行忽略大小写匹配
    可以通过设置flags参数为re.IGNORECASE或re.I来实现忽略大小写的匹配。

示例:

忽略大小写地匹配字符串中的"hello"。

import re
 
text = "Hello World"
pattern = r"hello"
match = re.search(pattern, text, re.IGNORECASE)
if match:
    print("匹配成功")
    print(match.group())  # 输出: Hello
else:
    print("匹配失败")

总结
通过上述示例,我们可以看到Python的re模块提供了强大的功能来处理字符串的匹配和替换。无论是简单的替换操作还是复杂的模式匹配,都可以通过正则表达式来高效地实现。掌握正则表达式的使用方法,将大大提高字符串处理的效率和准确性。

最后修改:2025 年 03 月 01 日
如果觉得我的文章对你有用,请随意赞赏