【Python基础入门 re模块实现正则表达式操作】

文章目录

1、匹配字符串

(1)使用match()方法进行匹配

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

从字符串的起始位置开始进行匹配,若开头的部分符合模式则返回匹配对象,否则返回 None

flags:I表示不区分大小写的匹配

bash 复制代码
# 从字符串的起始位置开始
import re
result = re.match(r'Hello', 'Hello world!')
print(result.group())

输出结果:Hello

不区分大小写的:

bash 复制代码
import re
result = re.match(r'Hello', 'hello world!', re.I)
print(result.group())

输出结果:hello

练习题:匹配中国移动手机号码

bash 复制代码
import re

pattern = r'(13[4-9]\d{8})$|(15[01289]\d{8})$'
mobile = '13634232323'
match = re.match(pattern, mobile)
if match is None:
    print(mobile, '不是有效的电话号码!')
else:
    print(mobile, '是有效的电话号码!')

关于pattern的解释:

|:这是一个逻辑或操作符,它将整个正则表达式分成了两部分,表示可以匹配左边的模式或者右边的模式。

(134-9\d{8})$:

13:直接匹配字符串 "13",代表中国移动的 13 号段。

4-9:匹配第三位数字,范围是 4 到 9,涵盖了 134 - 139 号段。

\d{8}:匹配后面的 8 位数字,这样整个号码长度就是 11 位。
KaTeX parse error: Undefined control sequence: \d at position 50: ...字符串。 (1501289\̲d̲{8}):

15:匹配字符串 "15",代表中国移动的 15 号段。

01289:匹配第三位数字,允许是 0、1、2、8、9,也就是 150、151、152、158、159 号段。

\d{8} 和 $:含义与前面相同,分别表示匹配 8 位数字和字符串结束位置。

(2)使用searc()方法进行匹配

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

在字符串中查找首个与模式匹配的位置,若匹配成功则返回一个匹配对象,若未找到则返回 None。相当于从字符串的任意位置开始匹配

bash 复制代码
# 检测是否出现了危险字符
import re
pattern = r'黑客|抓包|监听'
about = '我是一名程序员,我喜欢看计算机相关的图书,想研究下监听'
match = re.search(pattern, about)
if match is None:
    print(about, '@安全词汇')
else:
    print(about, '@出现了危险词汇:', match.group())

(3)使用findall()方法进行匹配

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

找出字符串中所有与模式匹配的非重叠子串,并以列表的形式返回。

bash 复制代码
import re

pattern = r'hello'
about = 'hello word Hello python hello java'
match = re.findall(pattern, about, re.I)
print(match)

运行结果:'hello', 'Hello', 'hello'

没找到返回空的列表:\[\]

2、替换字符串

bash 复制代码
import re

pattern = r'1[34578]\d{9}'
string = '中奖号码为:84978987 联系电话为:13612121212'
result = re.sub(pattern, '1**********', string)
print(result)

运行结果:中奖号码为:84978987 联系电话为:1**********

3、分割字符串

bash 复制代码
import re
pattern = r'[? | &]'
url = 'http://www.baidu.com/login.jsp?username="mr"&pwd="123456'
result = re.split(pattern, url)
print(result)
相关推荐
夏幻灵4 小时前
JavaScript this 面试:五种绑定规则、优先级与常见面试陷阱
开发语言·javascript·面试
程序猿编码4 小时前
榨干 RTX5090 算力!Qwen3 专用单卡推理引擎,手写C++/CUDA 算子实现 MTP 推测解码
开发语言·c++·大模型推理·qwen3·多模态推理
bkspiderx6 小时前
Qt 插件机制:动态扩展应用功能的核心框架
开发语言·qt·元数据·qt 插件·qpluginloader
程序员老陆6 小时前
深入理解 C++ thread_local:线程私有存储的正确打开方式
开发语言·c++·程序设计
沐晓时光6 小时前
C语言入门,深入理解指针(3)
c语言·开发语言
码云数智-园园6 小时前
unique_ptr 还是 shared_ptr?C++ 智能指针选型与内存泄漏实战分析
java·开发语言
禾小西7 小时前
07丨Redis 哨兵机制:主库故障后,如何恢复服务?
java·开发语言·redis
我不是阵雨7 小时前
JDK 21虚拟线程Pinning陷阱:一文拔钉解困
java·开发语言
全栈练习生7 小时前
AI Agent 沙箱
python·ai
徐小黑ACG8 小时前
Golang 基础05 结构体struct
开发语言·算法·golang