正交表达式
2016-02-13 11:29
316 查看
''' 匹配原则,匹配的数据 只能够接受字符串 ''' import re m=re.match('abc','abcdfe') print(m.group()) o=re.match('[0-9][0-9]','567ab123c5')#[0-9]只代表一个数字,这样子代表匹配两个数字 p=re.match('[0-9]{0,2}','3456ablc123d')#匹配【0-9】2位,一数字开头的,后面的不会被匹配,从头找,匹配到就返回 i=re.match('[0-9]{10}','1234abc') #匹配10位,这样会匹配失败 a=re.findall('[0-9]','a1d3f3d1a3d4f') b=re.findall('[0-9]{0,8}','a1d344444444444f3ggfgfgdfdfdd1a3d4f') #后面的表示{}表示匹配多少位,如果是8,表示匹配8位,表示0位到8位的数据都满足要求 c=re.findall('[0-9]{8}','a1d344444444444f3ggfgfgdfdfdd1a3d4f') #要求必须是8位 d=re.findall('.*','a1d344444444444f3ggfgfgdfdfdd1a3d4f') f=re.findall('.','''a1d344444444444f3ggfgfgdfdfdd1a3d4f sdfdsdfdfdfdsf''')#p匹配单行所有 print(o) f=re.findall('ab*',"""a1d344444444444f3ggfgfgdfdfdd1a3d4f sdfdsdfdfdfbdsf""")#匹配多行,必须是以a开头,可以是a #可以是ab G=re.findall('ab+','''a1d344444444444f3ggfgfgdfdfdd1a3d4f sdfdsdfdfdfdsf''')#匹配ab必须连在一起,匹配多个 i=re.findall('ab+','''ab1d34444444444ab4f3ggfgfgdfdfdd1a3d4f sdfdsdfdafdfdabcsf''')#匹配ab必须连在一起,匹配多个 H=re.findall('\S','''a1d34444444444~!4f3ggfgfgd## fdfdd1a3d4f sdfdsdfdfdfdf@#$fdsf''') j=re.findall('~','''a1d34444444444~!4f3ggfgfgd## fdfdd1a3d4f sdfdsdfdfdfdf@#$fdsf''') k=re.search('\d+','''a34d34444444444~!4f3ggfgfgd## fdfdd1a3d4f sdfdsdfdfdfdf@#$46.5fdsf''') l=re.sub('\d','G','''a34d34444444444~!4f3ggfgfgd## fdfdd1a3d4f sdfdsdfdfdfdf@#$46.5fdsf''')#会把所有的数字替换成大写的G m=re.sub('\d','G','''a34d34444444444~!4f3ggfgfgd## fdfdd1a3d4f sdfdsdfdfdfdf@#$46.5fdsf''',count=2)# 找到前两个进行替换 Q=re.sub('^\d','G','''a34d34444444444~!4f3ggfgfgd## fdfdd1a3d4f sdfdsdfdfdfdf@#$46.5fdsf''',count=2)# 找到一数字开头,这次会失败 n=re.search('^\d','''3a34d34444444444~!4f3ggfgfgd## fdfdd1a3d4f sdfdsdfdfdfdf@#$46.5fdsf''')# 找到一数字开头,这次会成功 N=re.search('^\d+$','''a34d34444444444~!4f3ggfgfgd## fdfdd1a3d4f sdfdsdfdfdfdf@#$46.5fdsf''')# 找到一数字开头,以数字结尾 print(o) print('ogoup',o.group()) print(p) print(p.group()) print(i) print(a) print('c',c) print('b',b) print(d) print(f) print('g',G) print('s',H) print('i',i) print('j',j) print('k',k) print('l',l) print('m',m) print('o',Q) print('n',n) print('N',N) 结果:
C:\Python34\python.exe "C:/Users/Admin/PycharmProjects/untitled1/第5 天/正交表达式.py"
abc
<_sre.SRE_Match object; span=(0, 2), match='56'>
<_sre.SRE_Match object; span=(0, 2), match='56'>
ogoup 56
<_sre.SRE_Match object; span=(0, 2), match='34'>
34
['ab', 'ab', 'ab']
['1', '3', '3', '1', '3', '4']
c ['34444444']
b ['', '1', '', '34444444', '4444', '', '3', '', '', '', '', '', '', '', '', '', '', '', '', '1', '', '3', '', '4', '', '']
['a1d344444444444f3ggfgfgdfdfdd1a3d4f', '']
['a', 'a']
g []
s ['a', '1', 'd', '3', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '~', '!', '4', 'f', '3', 'g', 'g', 'f', 'g', 'f', 'g', 'd', '#', '#', 'f', 'd', 'f', 'd', 'd', '1', 'a', '3', 'd', '4', 'f', 's', 'd', 'f', 'd', 's', 'd', 'f', 'd', 'f', 'd', 'f', 'd', 'f', '@', '#', '$', 'f', 'd', 's', 'f']
i ['ab', 'ab', 'ab']
j ['~']
k <_sre.SRE_Match object; span=(1, 3), match='34'>
l aGGdGGGGGGGGGGG~!GfGggfgfgd## fdfddGaGdGf
sdfdsdfdfdfdf@#$GG.Gfdsf
m aGGd34444444444~!4f3ggfgfgd## fdfdd1a3d4f
sdfdsdfdfdfdf@#$46.5fdsf
o a34d34444444444~!4f3ggfgfgd## fdfdd1a3d4f
sdfdsdfdfdfdf@#$46.5fdsf
n <_sre.SRE_Match object; span=(0, 1), match='3'>
N None
Process finished with exit code 0
相关文章推荐
- Android无序广播案例
- Ubuntu下10个厉害的Indicator小程序
- 红黑树
- git使用入门篇
- 用Docker运行Percona Server
- 都是以父元素的width为参照物的
- Javaio
- PAT1031
- java基础学习笔记之类加载器
- zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)
- PAT1008数组元素循环右移
- MAC 设置环境变量path的几种方法
- C#发送邮件时提示:“不允许使用邮箱名称。服务器响应为:”的错误解决办法
- uva10635 - Prince and Princess
- usaco 1.4.2 milk3
- 深度学习与人脸识别系列(2)__深度学习的环境搭建(ubuntu+caffe)
- phpstorm 常用设置
- Java设计模式(二十三)----解释器模式
- bzoj:1221;vijos 1552 软件开发
- EntityFramework简介