【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库的门槛,但正则表达式的奥秘还等待着你进一步挖掘。实践中如果遇到“明明规则写得对,为何匹配不上?”这类疑问,不妨回看本文,或是在留言区留下你的问题,我们一同探讨解惑,让正则表达式真正成为你手中的“文本魔法棒”。


目录
相关文章
|
存储 人工智能 测试技术
如何使用LangChain的Python库结合DeepSeek进行多轮次对话?
本文介绍如何使用LangChain结合DeepSeek实现多轮对话,测开人员可借此自动生成测试用例,提升自动化测试效率。
2326 125
如何使用LangChain的Python库结合DeepSeek进行多轮次对话?
|
12月前
|
监控 数据可视化 数据挖掘
Python Rich库使用指南:打造更美观的命令行应用
Rich库是Python的终端美化利器,支持彩色文本、智能表格、动态进度条和语法高亮,大幅提升命令行应用的可视化效果与用户体验。
1021 0
|
11月前
|
传感器 运维 前端开发
Python离群值检测实战:使用distfit库实现基于分布拟合的异常检测
本文解析异常(anomaly)与新颖性(novelty)检测的本质差异,结合distfit库演示基于概率密度拟合的单变量无监督异常检测方法,涵盖全局、上下文与集体离群值识别,助力构建高可解释性模型。
713 10
Python离群值检测实战:使用distfit库实现基于分布拟合的异常检测
|
11月前
|
数据可视化 关系型数据库 MySQL
【可视化大屏】全流程讲解用python的pyecharts库实现拖拽可视化大屏的背后原理,简单粗暴!
本文详解基于Python的电影TOP250数据可视化大屏开发全流程,涵盖爬虫、数据存储、分析及可视化。使用requests+BeautifulSoup爬取数据,pandas存入MySQL,pyecharts实现柱状图、饼图、词云图、散点图等多种图表,并通过Page组件拖拽布局组合成大屏,支持多种主题切换,附完整源码与视频讲解。
983 4
【可视化大屏】全流程讲解用python的pyecharts库实现拖拽可视化大屏的背后原理,简单粗暴!
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
752 18
|
12月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1755 102
|
12月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
554 104
|
12月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
453 103
|
12月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
464 82

推荐镜像

更多