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 = "a.b.e.f.g@qq.com.e.c"
result = re.findall(r,s)
print(result)
result = re.match(r,s)
print(result)

s = "a.b.e.f.g@123.com.e.c"
result = re.findall(r,s)
print(result)
result = re.match(r,s)
print(result)
相关推荐
Hy行者勇哥1 小时前
Python 与 VS Code 结合操作指南
开发语言·python
大力水手(Popeye)1 小时前
Pytorch——tensor
人工智能·pytorch·python
飞翔的佩奇5 小时前
【完整源码+数据集+部署教程】表盘指针检测系统源码和数据集:改进yolo11-CA-HSFPN
python·yolo·计算机视觉·数据集·yolo11·表盘指针检测
larance6 小时前
SQLAlchemy 的异步操作来批量保存对象列表
数据库·python
搏博6 小时前
基于Python3.10.6与jieba库的中文分词模型接口在Windows Server 2022上的实现与部署教程
windows·python·自然语言处理·flask·中文分词
麦兜*7 小时前
Swift + Xcode 开发环境搭建终极指南
开发语言·ios·swiftui·xcode·swift·苹果vision pro·swift5.6.3
lxmyzzs7 小时前
pyqt5无法显示opencv绘制文本和掩码信息
python·qt·opencv
萧鼎8 小时前
Python pyzmq 库详解:从入门到高性能分布式通信
开发语言·分布式·python
艾伦~耶格尔9 小时前
【集合框架LinkedList底层添加元素机制】
java·开发语言·学习·面试
yujkss9 小时前
Python脚本每天爬取微博热搜-终版
开发语言·python