Python基础知识_正则表达式(re)
off999 2024-11-18 15:35 21 浏览 0 评论
Python 的正则表达式(Regular Expressions,简称 regex)是通过 re 模块来实现的。正则表达式是一种强大的文本处理工具,可以用来进行搜索、替换、匹配等操作。以下是一些常用的 re 模块功能及其示例:
1.导入re模块
import re
2. 编写正则表达式
正则表达式是一种模式描述语言,用于匹配字符串中的字符组合。在Python中,正则表达式通常作为字符串传递给re模块的函数。
3. 使用re模块的函数
re模块提供了多个函数来处理正则表达式,其中最常用的包括:
- re.match(pattern, string, flags=0): 尝试从字符串的起始位置匹配正则表达式。
- re.search(pattern, string, flags=0): 扫描字符串,返回第一个匹配正则表达式的子串的Match对象。
- re.findall(pattern, string, flags=0): 返回字符串中所有匹配正则表达式的子串的列表。
- re.sub(pattern, repl, string, count=0, flags=0): 使用repl替换字符串中所有匹配正则表达式的子串。
- re.compile(pattern, flags=0): 编译一个正则表达式模式,返回一个Pattern对象。
4. 处理匹配结果
如果匹配成功,re.match()、re.search()和re.finditer()会返回一个Match对象,你可以使用这个对象来获取匹配的详细信息,比如匹配的子串、匹配的位置等。
基本匹配
1. 匹配字符串
pattern = r'hello'
text = 'hello world'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
else:
print('No match found')
2. 匹配数字
pattern = r'\d+'
text = 'There are 123 apples'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
else:
print('No match found')
特殊字符
1..匹配任意字符(除了换行符)
pattern = r'a.c'
text = 'abc abc123'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
2.^匹配字符串的开始
python
复制代码
pattern = r'^hello'
text = 'hello world'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
3.$匹配字符串的结束
python
复制代码
pattern = r'world#39;
text = 'hello world'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
字符集
1.[abc]匹配方括号内的任意一个字符
python
复制代码
pattern = r'[abc]'
text = 'abc def'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
2.[a-z]匹配方括号内的字符范围
python
复制代码
pattern = r'[a-z]'
text = 'A quick brown fox'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
量词
1.*匹配前面的字符零次或多次
python
复制代码
pattern = r'a*b'
text = 'aaab b'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
2.+匹配前面的字符一次或多次
python
复制代码
pattern = r'a+b'
text = 'aab b'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
3.?匹配前面的字符零次或一次
python
复制代码
pattern = r'a?b'
text = 'ab b'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
4.{n}匹配前面的字符恰好 n 次
python
复制代码
pattern = r'a{3}b'
text = 'aaab b'
match = re.search(pattern, text)
if match:
print('Match found:', match.group())
分组和捕获
1. 使用圆括号进行分组
python
复制代码
pattern = r'(\d+)-(\d+)-(\d+)'
text = 'My birthday is 1990-01-01'
match = re.search(pattern, text)
if match:
print('Match found:', match.group(0)) # 整个匹配
print('Year:', match.group(1))
print('Month:', match.group(2))
print('Day:', match.group(3))
替换
1. 使用re.sub进行替换
python
复制代码
pattern = r'\d+'
text = 'There are 123 apples and 456 oranges'
new_text = re.sub(pattern, 'XXX', text)
print(new_text) # 输出: There are XXX apples and XXX oranges
编译正则表达式
1. 使用re.compile编译正则表达式
python
复制代码
pattern = re.compile(r'\d+')
text = 'There are 123 apples'
match = pattern.search(text)
if match:
print('Match found:', match.group())
查找所有匹配项
1. 使用re.findall查找所有匹配项
python
复制代码
pattern = r'\d+'
text = 'There are 123 apples and 456 oranges'
matches = re.findall(pattern, text)
print(matches) # 输出: ['123', '456']
忽略大小写
1. 使用re.IGNORECASE忽略大小写
python
复制代码
pattern = r'hello'
text = 'Hello World'
match = re.search(pattern, text, re.IGNORECASE)
if match:
print('Match found:', match.group())
多行匹配
1. 使用re.MULTILINE进行多行匹配
python
复制代码
pattern = r'^hello'
text = '''hello
world
hello again'''
matches = re.findall(pattern, text, re.MULTILINE)
print(matches) # 输出: ['hello', 'hello']
相关推荐
- 编写更多 pythonic 代码(十三)——Python类型检查
-
一、概述在本文中,您将了解Python类型检查。传统上,类型由Python解释器以灵活但隐式的方式处理。最新版本的Python允许您指定显式类型提示,这些提示可由不同的工具使用,以帮助您更...
- [827]ScalersTalk成长会Python小组第11周学习笔记
-
Scalers点评:在2015年,ScalersTalk成长会完成Python小组完成了《Python核心编程》第1轮的学习。到2016年,我们开始第二轮的学习,并且将重点放在章节的习题上。Pytho...
- 用 Python 画一颗会跳动的爱心:代码里的浪漫仪式感
-
在编程的世界里,代码不仅是逻辑的组合,也能成为表达情感的载体。今天我们就来聊聊如何用Python绘制一颗「会跳动的爱心」,让技术宅也能用代码传递浪漫。无论是写给爱人、朋友,还是单纯记录编程乐趣,这...
- Python面向对象编程(OOP)实践教程
-
一、OOP理论基础1.面向对象编程概述面向对象编程(Object-OrientedProgramming,OOP)是一种编程范式,它使用"对象"来设计应用程序和软件。OOP的核心...
- 如何在 Python 中制作 GIF(python做gif)
-
在数据分析中使用GIF并发现其严肃的一面照片由GregRakozy在Unsplash上拍摄感谢社交媒体,您可能已经对GIF非常熟悉。在短短的几帧中,他们传达了非常具体的反应,只有图片才能传达...
- Python用内置模块来构建REST服务、RPC服务
-
1写在前面和小伙伴们分享一些Python网络编程的一些笔记,博文为《PythonCookbook》读书后笔记整理博文涉及内容包括:TCP/UDP服务构建不使用框架创建一个REST风格的HTTP...
- 第七章:Python面向对象编程(python面向对象六大原则)
-
7.1类与对象基础7.1.1理论知识面向对象编程(OOP)是一种编程范式,它将数据(属性)和操作数据的函数(方法)封装在一起,形成一个称为类(Class)的结构。类是对象(Object)的蓝图,对...
- 30天学会Python编程:8. Python面向对象编程
-
8.1OOP基础概念8.1.1面向对象三大特性8.1.2类与对象关系核心概念:类(Class):对象的蓝图/模板对象(Object):类的具体实例属性(Attribute):对象的状态/数据方法...
- RPython GC 对象分配速度大揭秘(废土种田,分配的对象超给力)
-
最近,对RPythonGC的对象分配速度产生了浓厚的兴趣。于是编写了一个小型的RPython基准测试程序,试图探究它对象分配的大致速度。初步测试与问题发现最初的设想是通过一个紧密循环来分配实...
- 30天学会Python编程:2. Python基础语法结构
-
2.1代码结构与缩进规则定义与原理Python使用缩进作为代码块的分界符,这是Python最显著的特征之一。不同于其他语言使用大括号{},Python强制使用缩进来表示代码层次结构。特性与规范缩进量...
- Python 类和方法(python类的方法与普通的方法)
-
Python类和方法Python类创建、属性和方法具体是如何体现的,代码中如何设计,请继续看下去。蟒蛇类解释在Python中使用OOP?什么是Python类?Python类创建Pyt...
- 动态类型是如何一步步拖慢你的python程序的
-
杂谈人人都知道python慢,这都变成了人尽皆知的事情了,但你知道具体是什么拖慢了python的运行吗?动态类型肯定要算一个!动态类型,能够提高开发效率,能够让我们更加专注逻辑开发,使得编程更加灵活。...
- 用Python让图表动起来,居然这么简单
-
我好像看到这个emoji:动起来了!编译:佑铭参考:https://towardsdatascience.com/how-to-create-animated-graphs-in-python-bb6...
- Python类型提示工程实践:提升代码质量的静态验证方案
-
根据GitHub年度开发者调查报告,采用类型提示的Python项目维护成本降低42%,代码审查效率提升35%。本文通过9个生产案例,解析类型系统在工程实践中的应用,覆盖API设计、数据校验、IDE辅助...
- Python:深度剖析实例方法、类方法和静态方法的区别
-
在Python中,类方法(classmethod)、实例方法(instancemethod)和静态方法(staticmethod)是三种不同类型的函数,它们在使用方式和功能上有一些重要的区别。理...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- 编写更多 pythonic 代码(十三)——Python类型检查
- [827]ScalersTalk成长会Python小组第11周学习笔记
- 用 Python 画一颗会跳动的爱心:代码里的浪漫仪式感
- Python面向对象编程(OOP)实践教程
- 如何在 Python 中制作 GIF(python做gif)
- Python用内置模块来构建REST服务、RPC服务
- 第七章:Python面向对象编程(python面向对象六大原则)
- 30天学会Python编程:8. Python面向对象编程
- RPython GC 对象分配速度大揭秘(废土种田,分配的对象超给力)
- 30天学会Python编程:2. Python基础语法结构
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python自定义函数 (53)
- python进度条 (67)
- python吧 (67)
- python字典遍历 (54)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python串口编程 (60)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python字典增加键值对 (53)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python人脸识别 (54)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)