正则表达式

正则表达式:

import re

s1 = 'python ui web 张三'

print(s1.find('web'))

print(re.search('web',s1)) #正则提供的精准查找

print(re.findall('web',s1)) #正则提供的模糊查找

t = '\d+'

print(re.findall(t,s1)) #通过正则查找字符串里的数字

#:t通配符:万能符,可以匹配\n以外的所有符号

s1 = 'tom 200 bom 308 do\nm 400 5e'

res1 = re.findall('0-9+',s1)

res2 = re.findall('0-9{1,}',s1)

res3 = re.findall('\d+',s1)

res4 = re.findall('\\d+',s1)

res5 = re.findall(r'\d+',s1)

res6 = re.findall(r'd|b+',s1)

res7 = re.findall(r'tbdom',s1)

res8 = re.findall(r'\n',s1)

res9 = re.findall(r'0-9a-zA-Z',s1)

print(res1)

print(res2)

print(res3)

print(res4)

print(res5)

print(res6)

print(res7)

print(res8)

print(res9)

s1 = 'aeeee age agree ape are a\ne ape a9e aFe'

reg = 'a.e'

res = re.findall(reg,s1)

res1 = re.findall('a.e',s1)

print(res1)

#2\[\]:表示字符串只能匹配一个

s2 = 'aeeee age agree ape are a\ne ape a9e aFe'

res1 = re.findall('ape',s1)

res2 = re.findall('apge',s1)

res3 = re.findall('a0-9e',s1)

res4 = re.findall('aa-ze',s1)

res5 = re.findall('aA-Ze',s1)

res6 = re.findall('aa-zA-Ze',s1)

res7 = re.findall('a\^0-9e',s1)

res8 = re.findall('a\^a-ze',s1)

print(res1)

print(res2)

print(res3)

print(res4)

print(res5)

print(res6)

print(res7)

print(res8)

#{}制定左侧可以重复的范围

s3 = 'aeeee age agree ape are a\ne ape a9e aFe'

res1 = re.findall('a...e',s3)

res2 = re.findall('a{3}e',s3)

#贪婪匹配规则,默认按照最大数量匹配

res6 = re.findall('a.{1,3}e',s3)

#取消贪婪匹配规则,在{}加?

res7 = re.findall('a.{1,3}?',s3)

#如果第二个值不写,默认无穷大

res8 = re.findall('a.{1,}',s3)

#更换匹配模式,匹配所有

res9 = re.findall('a.{1,}e',s3,re.S)

print(res6)

print(res7)

print(res8)

print(res9)

#?:制定左侧符号出现0次或1次,等同于{0,1},默认贪婪

s4 = 'aeeee age agree ape are a\ne ape a9e aFe'

res10 = re.findall('a.{0,1}?e',s4)

res11 = re.findall('a.??e',s4)

print(res10)

print(res11)

#*:制定左侧符号出现0次或多次,等同于{0,},默认贪婪

s4 = 'aeeee age agree ape are a\ne ape a9e aFe'

res10 = re.findall('a.{0,}?e',s4)

res11 = re.findall('a.*?e',s4)

print(res10)

print(res11)

#+:制定左侧符号出现1次或多次,等同于{1,},默认贪婪

s4 = 'aeeee age agree ape are a\ne ape a9e aFe'

res13 = re.findall('a.+?e',s4)

res12 = re.findall('0-9+?',s4)

print(res12)

print(res13)

s7 = '122223333', '13432416666','11011112222'

def test():

for i in num:

rees = re.findall(r'^13-9\d{9}$',n)

print(res)

test()

^与$

$以某个规则结尾

^以某个规则开头

s5 = '王哥王弟王嫂'

res12 = re.findall('王',s5)

res13 = re.findall('^王.',s5)

res14 = re.findall('王.$',s5)

相关推荐
小灰灰搞电子3 分钟前
Python 函数参数分隔符 *:Keyword-Only Arguments 原理与实践
开发语言·python
2601_962297255 分钟前
Authlib 0.13通用Python认证授权库wheel安装包(支持Python 2/3)
python·jwt·oauth2.0·authlib·openidconnect
SamChan906 分钟前
大文件多语言PDF翻译性能实测:300页文档的耗时、内存占用与失败率分析
python·ai·pdf·机器翻译
染的人9 分钟前
Java 10x15cm面单PDF转换A4格式PDF
java·开发语言·pdf
光电笑映16 分钟前
Linux 线程同步与互斥:锁的本质、条件变量与信号量的底层原理
linux·运维·服务器·开发语言·c++
GIS数据转换器17 分钟前
遥感GIS一体化技术应用平台
大数据·人工智能·python·安全·数据挖掘
Json____25 分钟前
从零构建在线拍卖系统:Java 全栈开发实践
java·开发语言·管理系统·it学习·wwwoop.com
2601_9669496527 分钟前
批量 K 线不只是提速:因子研究中的数据质量、复权与回测偏差
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
matlab代码28 分钟前
基于matlab水果识别系统(西红柿、香蕉、梨、青椒)【源码64期】
开发语言·matlab
一位正在转型AI全栈的前端工程师28 分钟前
AI 全栈学习之旅 -Week 11:从 CLI 到浏览器:用 FastAPI、SSE 和 Vue 3 做一个可审核的 ReAct Agent
前端·python