【Python-正则表达式】

Python-正则表达式

  • [■ 基础匹配](#■ 基础匹配)
    • [■ match 从头匹配](#■ match 从头匹配)
    • [■ search 搜索匹配](#■ search 搜索匹配)
    • [■ findall 搜索全部匹配](#■ findall 搜索全部匹配)
    • [■ 综合](#■ 综合)
  • [■ 元字符匹配](#■ 元字符匹配)
    • [■ r'b-eF-Z3-9' r表示意思](#■ r'[b-eF-Z3-9]' r表示意思)
    • [■ 正则表达式使用元字符进行匹配](#■ 正则表达式使用元字符进行匹配)
    • [■ 匹配账号,只能由字母和数字组成,长度限制6到10位](#■ 匹配账号,只能由字母和数字组成,长度限制6到10位)
    • [■ 匹配QQ号,要求纯数字,长度5-11,第一位不为0](#■ 匹配QQ号,要求纯数字,长度5-11,第一位不为0)
    • [■ 匹配邮箱地址,只允许qq、163、gmail这三种邮箱地址](#■ 匹配邮箱地址,只允许qq、163、gmail这三种邮箱地址)
    • [■ 综合](#■ 综合)

■ 基础匹配

■ match 从头匹配

python 复制代码
import re
s = "1python itheima python python"
# match 从头匹配
result = re.match("python", s)
print(result)
python 复制代码
import re
s = "1python itheima python python"
# search 搜索匹配
result = re.search("python2", s)
print(result)

■ findall 搜索全部匹配

python 复制代码
import re
s = "1python itheima python python"
# findall 搜索全部匹配
result = re.findall("python", s)
print(result)

■ 综合

python 复制代码
"""
演示Python正则表达式re模块的3个基础匹配方法
"""
import re

s = "1python itheima python python"
# match 从头匹配
result = re.match("python", s)
print(result)
# print(result.span())
# print(result.group())
# search 搜索匹配

result = re.search("python2", s)
print(result)
# findall 搜索全部匹配
result = re.findall("python", s)
print(result)

■ 元字符匹配

■ r'b-eF-Z3-9' r表示意思

字符串前面带上r的标记,表示字符串中转义字符无效,就是普通字符的意思

■ 正则表达式使用元字符进行匹配

python 复制代码
"""
演示Python正则表达式使用元字符进行匹配
"""
import re
# s = "itheima1 @@python2 !!666 ##itccast3"
#
# result = re.findall(r'[b-eF-Z3-9]', s)   # 字符串前面带上r的标记,表示字符串中转义字符无效,就是普通字符的意思
# print(result)

■ 匹配账号,只能由字母和数字组成,长度限制6到10位

python 复制代码
# r = '^[0-9a-zA-Z]{6,10}$'
# s = '123456_'
# print(re.findall(r, s))

■ 匹配QQ号,要求纯数字,长度5-11,第一位不为0

python 复制代码
r = '^[1-9][0-9]{4,10}$'
s = '123453678'
print(re.findall(r, s))

■ 匹配邮箱地址,只允许qq、163、gmail这三种邮箱地址

python 复制代码
# abc.efg.daw@qq.com.cn.eu.qq.aa.cc
# abc@qq.com
# {内容}.{内容}.{内容}.{内容}.{内容}.{内容}.{内容}.{内容}@{内容}.{内容}.{内容}
r = r'(^[\w-]+(\.[\w-]+)*@(qq|163|gmail)(\.[\w-]+)+$)'
# s = 'a.b.c.d.e.f.g@qq.com.a.z.c.d.e'
s = 'a.b.c.d.e.f.g@126.com.a.z.c.d.e'
print(re.match(r, s))

■ 综合

python 复制代码
"""
演示Python正则表达式使用元字符进行匹配
"""
import re

# s = "itheima1 @@python2 !!666 ##itccast3"
#
# result = re.findall(r'[b-eF-Z3-9]', s)   # 字符串前面带上r的标记,表示字符串中转义字符无效,就是普通字符的意思
# print(result)

# 匹配账号,只能由字母和数字组成,长度限制6到10位
# r = '^[0-9a-zA-Z]{6,10}$'
# s = '123456_'
# print(re.findall(r, s))

# 匹配QQ号,要求纯数字,长度5-11,第一位不为0
# r = '^[1-9][0-9]{4,10}$'
# s = '123453678'
# print(re.findall(r, s))

# 匹配邮箱地址,只允许qq、163、gmail这三种邮箱地址
# abc.efg.daw@qq.com.cn.eu.qq.aa.cc
# abc@qq.com
# {内容}.{内容}.{内容}.{内容}.{内容}.{内容}.{内容}.{内容}@{内容}.{内容}.{内容}
r = r'(^[\w-]+(\.[\w-]+)*@(qq|163|gmail)(\.[\w-]+)+$)'
# s = 'a.b.c.d.e.f.g@qq.com.a.z.c.d.e'
s = 'a.b.c.d.e.f.g@126.com.a.z.c.d.e'
print(re.match(r, s))
相关推荐
学无止境小袁同学6 小时前
Google ADK 学习指南
python·llm·agent·多agent·google adk·llmagent·agent cli
郝学胜-神的一滴7 小时前
Effective Python 条款35:用类封装替代生成器异常注入
开发语言·人工智能·python·金融·pandas·scikit-learn·matplotlib
LK_077 小时前
【力扣】142. 环形链表 II |Python
python·leetcode·链表
wuyk5557 小时前
Python办公自动化-第02讲-Excel文件认知与四大库选型
开发语言·python·excel
用户54817845909508 小时前
Java 与 Python 在大模型与智能体领域的选型
java·python
yiangL_9 小时前
1、Python 输入输出实验|print 与 input 人机交互实践
开发语言·python·人机交互
高洁019 小时前
世界模型的因果内核:从预测表象到理解机制
人工智能·python·深度学习·transformer·迁移学习
数字融合10 小时前
透明化视频动态三维重建空间数据零核心技术
人工智能·python·virtualenv
AI创界者10 小时前
Python 进阶实战(五十四章):大模型推理网关终极架构——分布式全局前缀 Trie 缓存同步与多网关高可用协同调度
人工智能·python·aigc