Python中re模块的使用

在Python中,处理正则表达式的模块是re模块。通过re模块,可以使用正则表达式来进行字符串的匹配、查找、替换等操作。执行效率特别高,可读性不强。

1.方法

match 从头开始匹配 返回Match 或者None

python 复制代码
import re
r = re.match(".\w{10}", "Hello_world hi world", re.I)
print(type(r), r)
if r:
    print(r.group())

search 匹配整个字符串 返回Match 或者None

python 复制代码
r = re.search(r"\d", "1a2b3c")
print(type(r), r)
if r:
    print(r.group())

fullmatch 从头开始匹配 返回Match 或者None

python 复制代码
r = re.fullmatch(r"\d", "12345")
print(type(r), r)
if r:
    print(r.group())

finditer 找到所有,返回迭代器,其中的每一个元素都是Match

python 复制代码
r = re.finditer(r"\d", "1a2b3c4d")
print(r, type(r))

findall 找到所有,返回列表或者空列表

split 切割,返回列表

sub 替换,返回字符串

subn 替换,返回元组(新字符串,替换个数)

python 复制代码
r = re.split(r"\d", "1a2b3c4d", 2)
print(r, type(r))

r = re.sub(r"\d", "+", "1a2b3c4d5e6f7g", 2)
print(r, type(r))

r = re.subn(r"\d", "+", "1a2b3c4d5e6f7g", 2)
print(r, type(r))

2.字符匹配

. 匹配任意字符

\d 匹配数字

\D 非数字

\w 字母数字下划线

\W 非字母数字下划线

\s 空白字符 :空格 制表符 换行符

\S 非空白字符

python 复制代码
r = re.findall(r".", "hello world+-*/")
r = re.findall(r"\d\D", "he1llo33 wor5ld+-*/")
r = re.findall(r"\w\W", "hel.lo33_wor_ld+-*/")
r = re.findall(r"\s\S", "hell o33\n world+-*/")

print(type(r), r)

3.重复

* 出现0-n次

  • 有1-n次

? 有0或1个

.* 默认是贪婪模式(尽可能多匹配) .*? 非贪婪模式(尽可能少匹配)

{n}匹配n次

{m,n}匹配m-n次

python 复制代码
import re
r = re.findall(r"a*b", "aaabcabcdc")
print(r, type(r))

r = re.findall(r"a+b", "aaabcabcdc")
print(r, type(r))

r = re.findall(r"a?", "aaabcabcdc")
print(r, type(r))

r = re.findall(r".*?", "aaabcabcdc")
print(r, type(r))

r = re.findall(r"\d{2}", "12345")
print(r, type(r))

r = re.findall(r"\d{3,5}", "123456789")
print(r, type(r))

4.边界

^ 以开头

$ 以结尾

\b 匹配单词边界

\B 匹配非单词边界

python 复制代码
r = re.findall(r"^.*?\b$", "hello world\tlove China")
print(r, type(r))

5.标识符

re.I 忽略大小写

re.M 多行模式,如果有换行符

re.S 单行模式

python 复制代码
r = re.findall(r"^a.*?d$", "abcd\n1a2b3c4d\n+-*/", re.M)
print(r, type(r))

6.特殊字符

abcdefg\] 只能取一个 \[\^abcdefg\] 不在abcdefg中间 \[a-z0-9A-Z\] 所有数字字母下划线 相当于\\w ```python import re r = re.findall(r"a[bc]d", "abdacdaedafd") print(r, type(r)) r = re.findall(r"^abc", "abdacdabcdedafd") print(r, type(r)) r = re.findall(r"a[a-z0-9A-Z_]d", "abdacdaedafda1da2da3da9daMdaZd") print(r, type(r)) ``` () 分组 \\n 取前面的分组匹配的内容 ( \| ) 分组 ```python import re r = re.findall(r"()", "abcd1234abc123") print(r, type(r)) r = re.findall(r"(\d\d|ab)-\d", "13-24ab-2a") print(r, type(r)) ```

相关推荐
wheeldown6 分钟前
【Linux】Linux 进程通信:System V 共享内存(最快方案)C++ 封装实战 + 通信案例,4 类经典 Bug 快速修复
linux·运维·服务器·开发语言
小年糕是糕手16 分钟前
【数据结构】双向链表“0”基础知识讲解 + 实战演练
c语言·开发语言·数据结构·c++·学习·算法·链表
Q_Q51100828517 分钟前
python+uniapp基于微信小程序的学院设备报修系统
spring boot·python·微信小程序·django·flask·uni-app
将车24421 分钟前
C++实现二叉树搜索树
开发语言·数据结构·c++·笔记·学习
蓝色空白的博客32 分钟前
自动化测试脚本-->集成测试部署思路整理(1)
python·集成测试
梵得儿SHI37 分钟前
Java 反射机制核心类详解:Class、Constructor、Method、Field
java·开发语言·反射·class·constructor·java反射·java反射机制
Blossom.1181 小时前
把AI“绣”进丝绸:生成式刺绣神经网络让古装自带摄像头
人工智能·pytorch·python·深度学习·神经网络·机器学习·fpga开发
hbqjzx1 小时前
记录一个自动学习的脚本开发过程
开发语言·javascript·学习
星星也在雾里1 小时前
【管理多版本Python环境】Anaconda安装及使用
python·anaconda
Sirens.1 小时前
Java核心概念:抽象类、接口、Object类深度剖析
java·开发语言·github