python 正则表达式

复制代码
import re

s = "hello world"
result = re.match("hello",s)
print(result)
print(result.span())
print(result.group())
复制代码
import re

s = "hello world"
result = re.match("hello",s)
print(result)
print(result.span())
print(result.group())
s = "good hello world"
result = re.search("hello",s)
print(result)


s = "good hello world  hello earth"
result = re.findall("hello",s)
print(result)

复制代码
import re

s = "hello world"
result = re.match("hello",s)
print(result)
print(result.span())
print(result.group())
s = "good hello world"
result = re.search("hello",s)
print(result)


s = "good hello world  hello earth"
result = re.findall("hello",s)
print(result)

s = "aaajkjl dja123jkjhj#hjkhd3444@"
result = re.findall(r'\d',s)#r 表示是正常子符
print(result)
result = re.findall(r'\W',s)# 特殊字符
print(result)

result = re.findall(r'[a-zA-Z 2-3]',s)
print(result)

#^ 开头 结尾
r = '^[0-9a-zA-Z]{6,10}$'
#满足条件
s = "123456yys"
result = re.findall(r,s)
print(result)
#不满足条件
s = "123456yys*"
result = re.findall(r,s)
print(result)


r = '^[1-9][0-9]{4,10}$'
#满足条件
s = "35878986"
result = re.findall(r,s)
print(result)
#不满足条件
s = "035878986"
result = re.findall(r,s)
print(result)

r = r'(^[\w-]+(\.[\w-]*)*@(qq|163|gmail)(\.[\w-]*)+$)'
s = "[email protected]"
result = re.findall(r,s)
print(result)
result = re.match(r,s)
print(result)

s = "[email protected]"
result = re.findall(r,s)
print(result)
result = re.match(r,s)
print(result)
相关推荐
lizz6664 分钟前
Python查询ES错误ApiError(406, ‘Content-Type ...is not supported
python·elasticsearch
lqjun082710 分钟前
Focal Loss 原理详解及 PyTorch 代码实现
人工智能·pytorch·python
神仙别闹19 分钟前
基于C#+MySQL实现(WinForm)企业设备使用信息管理系统
开发语言·mysql·c#
Kazefuku25 分钟前
python文件打包成exe文件
python·学习
czhaii30 分钟前
PLC脉冲位置 单片机跟踪读取记录显示
开发语言·c#
alden_ygq1 小时前
当java进程内存使用超过jvm设置大小会发生什么?
java·开发语言·jvm
源码方舟1 小时前
【基于ALS模型的教育视频推荐系统(Java实现)】
java·python·算法·音视频
蜗牛沐雨1 小时前
Rust 中的 `PartialEq` 和 `Eq`:深入解析与应用
开发语言·后端·rust
Python私教1 小时前
Rust快速入门:从零到实战指南
开发语言·后端·rust
萑澈1 小时前
2025深圳杯D题法医物证多人身份鉴定问题四万字思路
python·数学建模