【Python进阶必备】一文掌握re库:实战正则表达式

简介: 【Python进阶必备】一文掌握re库:实战正则表达式

亲爱的读者,你是否在编程过程中遇到过字符串处理难题?是否对繁琐复杂的文本匹配操作感到困扰?今天,我们就一起深入探索Python世界中的强大工具——re模块,它是Python标准库中用于处理正则表达式的利器,帮你轻松驾驭各类字符串处理任务。

re库初识

Python的re模块提供了完整的正则表达式功能。正则表达式(Regular Expression)是一种强大的文本模式匹配工具,它能高效地进行查找、替换、分割等复杂字符串操作。

在Python中,通过 import re 即可引入这一神器。

re库基础使用方法

compile()函数

首先,我们需要使用re.compile()函数将正则表达式编译为Pattern对象

基本用法

import re
 
# 匹配一个或多个连续的数字字符
pattern = re.compile(r'\d+') 
 
# 匹配email电邮地址
email_pattern = re.compile(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', re.IGNORECASE)
 
# 匹配任意字母数字组成的用户名(至少1个字符)
username_pattern = re.compile(r'\w+')
 
# 匹配任意URL链接
url_pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
 
# 匹配电话号码(格式如:123-456-7890 或 (123) 456-7890)
phone_pattern = re.compile(r'(\d{3}[-\.\s]??\d{3}[-\.\s]??\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]??\d{4})')
 
# 匹配IPv4地址
ipv4_pattern = re.compile(r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)')
 
# 匹配信用卡号(一般为16位数字,可能包含空格分隔符)
credit_card_pattern = re.compile(r'\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}')
 
# 匹配日期格式(YYYY-MM-DD)
date_pattern = re.compile(r'\d{4}-\d{2}-\d{2}')
 
# 匹配颜色代码(如 #FF0000)
color_code_pattern = re.compile(r'^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')
 
# 匹配整数和小数(包括负数、正数和零)
number_pattern = re.compile(r'-?\d+(\.\d+)?')

正则表达式常用规则字符

  • \d:在大多数正则表达式语法中(包括Python中的 re 模块),\d 相当于 [0-9],即它会匹配任意一个十进制数字字符,相当于阿拉伯数字从0到9。
  • +:这是一个量词,表示前面的元素(这里是\d)至少出现一次或多次。因此,\d+ 作为一个整体,它会匹配一个或连续的一个以上数字字符,例如 "123"、"456789" 等等。
  • \w:匹配字母(大写或小写)、数字和下划线(等价于 [a-zA-Z0-9_])。
  • \s:匹配任何空白字符,包括空格、制表符、换行符等。
  • . (句点):匹配除换行符之外的任何单个字符。
  • ^:在字符串起始位置时匹配,或者在字符类 [] 中表示反向选择(如 [^abc] 匹配非 a、b、c 的字符)。
  • $:在字符串结束位置时匹配。
  • *:零次或多次匹配前面的元素。
  • ?:零次或一次匹配前面的元素。
  • {m,n}:前面的元素至少出现 m 次,至多出现 n 次。
  • |:表示“或”操作,用于匹配多个选项之一。
  • ():用于分组和捕获子匹配项。

re.compile(pattern, flags=0) 的作用是:

  1. 预编译:将正则表达式转换为编译过的模式对象,提高后续匹配操作的速度。
  1. 复用:创建一次编译好的模式后,可以在程序的不同地方重复使用该模式进行匹配、查找、替换等操作。
  2. 支持标志:可以传递标志参数来改变正则表达式的默认行为,如忽略大小写、多行模式等。

match与search方法

pattern.match()方法只检测字符串开始位置是否满足匹配条件;而pattern.search()方法会搜索整个字符串以找到第一个匹配项。

match

import re
 
text = "2023-01-01 This is a date at the start of the string."
 
# 使用match()方法,只从字符串开始位置匹配日期格式
pattern = re.compile(r'\d{4}-\d{2}-\d{2}')
match_result = pattern.match(text)
 
if match_result:
    print(f"Match found: {match_result.group(0)}")
else:
    print("No match at the beginning of the string.")
 
# 输出:
# Match found: 2023-01-01
import re
 
text = "The date today is 2023-01-01, let's remember it."
 
# 使用search()方法在整个字符串中搜索日期格式
pattern = re.compile(r'\d{4}-\d{2}-\d{2}')
search_result = pattern.search(text)
 
if search_result:
    print(f"Search found: {search_result.group(0)}")
else:
    print("No match found in the string.")
 
# 输出:
# Search found: 2023-01-01

match/search

import re
 
text = "This sentence does not start with a date like 2023-01-01."
 
# match()不会找到任何匹配项,因为日期不在字符串开头
match_result = re.match(r'\d{4}-\d{2}-\d{2}', text)
if match_result:
    print("Match found.")
else:
    print("No match at the beginning using match().")
 
# search()能找到匹配项,因为它搜索整个字符串
search_result = re.search(r'\d{4}-\d{2}-\d{2}', text)
if search_result:
    print("Search found.")
else:
    print("No match found anywhere using search().")
 
# 输出:
# No match at the beginning using match().
# Search found.

findall与finditer方法

pattern.findall()返回所有非重叠匹配结果的列表;pattern.finditer()返回一个迭代器,逐个返回Match对象。

使用findall()返回所有匹配项

import re
 
text = "The3 quick5 brown5 fox3 jumps5 over4 the3 lazy4 dog."
 
# 找到文本中所有的"fox"
pattern = re.compile(r'\d+')
matches = pattern.findall(text)
 
print(matches)
 
# 输出: ['3', '5', '5', '3', '5', '4', '3', '4']

使用findall()提取多个组的匹配

import re
 
text = "John Doe, Jane Smith, Alice Johnson"
 
# 提取所有名字和姓氏
pattern = re.compile(r'(\w+) (\w+)')
matches = pattern.findall(text)
 
print(matches)
 
# 输出: [('John', 'Doe'), ('Jane', 'Smith'), ('Alice', 'Johnson')]
 
# 返回的是元组组成的列表,每个元组代表一个匹配的结果,其中包含了括号分组的内容

使用finditer()逐个返回Match对象

import re
 
text = "I have 3 apples and 7 bananas in 2 baskets."
 
# 查找所有数字
pattern = re.compile(r'\d+')
 
for match in pattern.finditer(text):
    print(match.group(0))
 
# 输出:
# 3
# 7
# 2
# finditer()方法逐个返回Match对象,并可以通过group()方法获取匹配的具体内容

使用finditer()并处理复杂匹配结构

import re
 
text = "colors: red, colors:blue; shapes: square, shapes:circle"
 
# 匹配颜色或形状
pattern = re.compile(r'(?:colors?[:\s]+(\w+)(?:[,;\s]|$))|(?:shapes?[:\s]+(\w+)(?:[,;\s]|$))')
 
for match in pattern.finditer(text):
    if match.group(1):  # 如果是颜色
        print(f"Color found: {match.group(1)}")
    elif match.group(2):  # 如果是形状
        print(f"Shape found: {match.group(2)}")
 
# 输出:
# Color found: red
# Color found: blue
# Shape found: square
# Shape found: circle

进阶用法

分组与反向引用

通过圆括号可以创建子组,以便捕获和引用部分匹配内容。如re.compile(r'(\w+) (\d+)')\1\2分别代表第一个和第二个子组的内容。

替换文本中的部分内容

import re
 
text = "John Doe has 3 apples and Jane Smith has 7 bananas."
pattern = re.compile(r'(\w+) (\d+)')
new_text = pattern.sub(r'\1 has \2 fruits', text)
 
print(new_text)
# 输出: "John Doe has 3 fruits and Jane Smith has 7 fruits."
 
# 在这个例子中,\1 替换为第一个子组(名字),\2 替换为第二个子组(数字)

提取并重组子组

import re
 
text = "The date is 2023-01-01, and the time is 15:30:45."
pattern = re.compile(r'(\d{4})-(\d{2})-(\d{2})')
 
match = pattern.search(text)
if match:
    date_reformatted = f"{match.group(1)}.{match.group(2)}.{match.group(3)}"
    print(date_reformatted)
    # 输出: "2023.01.01"
 
    # 这里直接通过group()方法获取每个子组的内容,并重新组合

在搜索结果中使用子组

import re
 
text = "Some emails are user1@exam.com, user2@apple.net, and user3@example.org."
pattern = re.compile(r'([\w.%+-]+)@([\w.-]+)\.([a-z]{2,})')
 
matches = pattern.findall(text)
for email in matches:
    username, domain, dtype = email[0], email[1], email[2]
    print(f"Username: {username}, Domain: {domain}.{dtype}")
    
    # 使用子组匹配的邮箱用户名和域名
    # 输出:
    # Username: user1, Domain: exam.com
    # Username: user2, Domain: apple.net
    # Username: user3, Domain: example.org

贪婪与懒惰匹配

*+?后添加?可变为非贪婪模式,尽可能少地匹配字符。

贪婪与非贪婪的 * 量词

import re
 
text = "I love Python programming and Java programming very much!"
 
# 贪婪模式
pattern_greedy = re.compile(r'love.*programming')
match_greedy = pattern_greedy.search(text)
print(match_greedy.group(0))  # 输出: 'love Python programming and Java programming'
 
# 非贪婪模式
pattern_lazy = re.compile(r'love.*?programming')
match_lazy = pattern_lazy.search(text)
print(match_lazy.group(0))  # 输出: 'love Python programming'

贪婪与非贪婪的 + 量词

import re
 
text = "The numbers are 139-626 and 123456."
 
# 贪婪模式
pattern_greedy = re.compile(r'\d+')
matches_greedy = pattern_greedy.findall(text)
print(matches_greedy)
# 输出: ['139', '626', '123456']
 
# 非贪婪模式
pattern_lazy = re.compile(r'\d+?')
matches_lazy = pattern_lazy.findall(text)
print(matches_lazy)
# 输出: ['1', '3', '9', '6', '2', '6', '1', '2', '3', '4', '5', '6']

 贪婪与非贪婪的 ? 量词

import re
 
text = "Optional text or not?"
 
# 贪婪模式
pattern_greedy = re.compile(r'(Optional)?.*')
match_greedy = pattern_greedy.search(text)
print(match_greedy.group(0))  # 输出: 'Optional text or not?'
 
# 非贪婪模式
pattern_lazy = re.compile(r'(Optional)?.*?')
match_lazy = pattern_lazy.search(text)
print(match_lazy.group(0))  # 输出: 'Optional'

预定义字符集与特殊字符

\d\D\w\W\s\S分别代表数字、非数字、单词字符、非单词字符、空白符、非空白符。


结语与讨论

正则表达式和re库的强大远不止于此,其深度和灵活性足以应对各种复杂的文本处理场景。然而,掌握好这门艺术需要不断的实践和积累,本文只是带你踏入了Python re库的门槛,但正则表达式的奥秘还等待着你进一步挖掘。实践中如果遇到“明明规则写得对,为何匹配不上?”这类疑问,不妨回看本文,或是在留言区留下你的问题,我们一同探讨解惑,让正则表达式真正成为你手中的“文本魔法棒”。


目录
相关文章
|
2天前
|
Python
在Python中绘制K线图,可以使用matplotlib和mplfinance库
使用Python的matplotlib和mplfinance库可绘制金融K线图。mplfinance提供便利的绘图功能,示例代码显示如何加载CSV数据(含开盘、最高、最低、收盘价及成交量),并用`mpf.plot()`绘制K线图,设置类型为'candle',显示移动平均线(mav)和成交量信息。可通过调整参数自定义图表样式,详情参考mplfinance文档。
12 2
|
3天前
|
机器学习/深度学习 边缘计算 TensorFlow
【Python机器学习专栏】Python机器学习工具与库的未来展望
【4月更文挑战第30天】本文探讨了Python在机器学习中的关键角色,重点介绍了Scikit-learn、TensorFlow和PyTorch等流行库。随着技术进步,未来Python机器学习工具将聚焦自动化、智能化、可解释性和可信赖性,并促进跨领域创新,结合云端与边缘计算,为各领域应用带来更高效、可靠的解决方案。
|
3天前
|
机器学习/深度学习 数据采集 TensorFlow
【Python机器学习专栏】使用Python进行图像分类的实战案例
【4月更文挑战第30天】本文介绍了使用Python和深度学习库TensorFlow、Keras进行图像分类的实战案例。通过CIFAR-10数据集,展示如何构建和训练一个卷积神经网络(CNN)模型,实现对10个类别图像的识别。首先安装必要库,然后加载数据集并显示图像。接着,建立基本CNN模型,编译并训练模型,最后评估其在测试集上的准确性。此案例为初学者提供了图像分类的入门教程,为进一步学习和优化打下基础。
|
3天前
|
Serverless Python
使用Python的pandas和matplotlib库绘制移动平均线(MA)示例
使用Python的pandas和matplotlib库绘制移动平均线(MA)示例:加载CSV数据,计算5日、10日和20日MA,然后在K线图上绘制。通过`rolling()`计算平均值,`plot()`函数展示图表,`legend()`添加图例。可利用matplotlib参数自定义样式。查阅matplotlib文档以获取更多定制选项。
14 1
|
3天前
|
数据采集 SQL 数据挖掘
Python数据分析中的Pandas库应用指南
在数据科学和分析领域,Python语言已经成为了一种非常流行的工具。本文将介绍Python中的Pandas库,该库提供了强大的数据结构和数据分析工具,使得数据处理变得更加简单高效。通过详细的示例和应用指南,读者将了解到如何使用Pandas库进行数据加载、清洗、转换和分析,从而提升数据处理的效率和准确性。
|
3天前
|
SQL 关系型数据库 MySQL
使用Python的pymysql库连接MySQL,执行CRUD操作
使用Python的pymysql库连接MySQL,执行CRUD操作:安装pymysql,然后连接(host='localhost',user='root',password='yourpassword',database='yourdatabase'),创建游标。查询数据示例:`SELECT * FROM yourtable`;插入数据:`INSERT INTO yourtable...`;更新数据:`UPDATE yourtable SET...`;删除数据:`DELETE FROM yourtable WHERE...`。
10 0
|
4天前
|
机器学习/深度学习 算法 数据挖掘
机器学习--K近邻算法,以及python中通过Scikit-learn库实现K近邻算法API使用技巧
机器学习--K近邻算法,以及python中通过Scikit-learn库实现K近邻算法API使用技巧
|
4天前
|
存储 Python Windows
轻松学会openpyxl库,Python处理Excel有如神助
轻松学会openpyxl库,Python处理Excel有如神助
|
4天前
|
NoSQL Python
在Python中,我们可以使用许多库来处理Excel文件
Python处理Excel常用pandas和openpyxl库。pandas的`read_excel`用于读取文件,`to_excel`写入;示例展示了数据框操作。openpyxl则用于处理复杂情况,如多工作表,`load_workbook`加载文件,`iter_rows`读取数据,`Workbook`创建新文件,写入单元格数据后保存。
12 1
|
4天前
|
Python
使用Seaborn库创建图形的使用案例
【4月更文挑战第29天】该代码段首先导入seaborn和matplotlib库,然后加载名为"titanic"的数据集。接着,它创建一个画布并设定子图大小。通过seaborn的FacetGrid以"Attrition_Flag"为列进行分组,映射数据到网格上,用histplot展示"Customer_Age"的直方图分布。同样,也使用boxplot方法生成"Freq"的箱线图。最后展示所有图形。
8 2