30-Python3 正则表达式
30-Python3 正则表达式
- '''
- re.match函数
- '''
- import re
- print(re.match('www','www.runoob.com').span())
- print(re.match('ww','www.runoob.com').span())
- print(re.match('w','www.runoob.com').span())
- print(re.match('com','www.runoob.com'))
- line = 'Cats are smarter than dogs'
- macthObj = re.match(r'(.*)are(.*?).*',line,re.M|re.I)
- if macthObj:
- print('matchObj.group():',macthObj.group())
- print('matchObj.group(1):',macthObj.group(1))
- print('matchObj.group(2):',macthObj.group(2))
- else:
- print('No match')
- '''
- re.search方法
- '''
- import re
- print(re.search('www','www.runoob.com').span())
- print(re.search('com','www.runoob.com').span())
- line1 = 'Cats are smarter than dogs'
- searchObj = re.search(r'(.*)are(.*?).*',line1,re.M|re.I)
- if searchObj:
- print('searchObj.group():',searchObj.group())
- print('searchObj.group(1):',searchObj.group(1))
- print('searchObj.group(2):',searchObj.group(2))
- else:
- print('Nothing found!')
- '''
- re.match和re.search的区别
- '''
- line2 = 'Cats are smarter than dogs'
- matchObj = re.match(r'dogs',line2,re.M|re.I)
- if matchObj:
- print('re.match:',matchObj.group())
- else:
- print('no match1')
- matchObj = re.search(r'dogs',line2,re.M|re.I)
- if matchObj:
- print('re.search:',matchObj.group())
- else:
- print('no match2')
- '''
- 检索和替换
- '''
- phone = '2004-959-559 #这是一个电话号码'
- ##删除注释
- num = re.sub(r'#.*$','',phone)
- print('电话号码1:',num)
- ##移除非数字的内容
- num = re.sub(r'\D','',phone)
- print('电话号码2:',num)
- '''
- repl参数是一个函数
- '''
- #将匹配到到数字乘以2
- def double(matched):
- value = int(matched.group('value'))
- return str(value*2)
- s = 'QAA342RFDFD56FGFG'
- print(re.sub('(?P<value>\d+)',double,s))
- '''
- compile函数
- '''
- pattern1 = re.compile(r'\d+')
- m = pattern1.match('one12twothree34four')
- m1 = pattern1.search('one12twothree34four')
- print('m',m)
- print('m1',m1)
- m2 =pattern1.match('one12twothree34four',2,10)
- print('m2',m2)
- m3 =pattern1.match('one12twothree34four',3,10)
- print('m3:',m3)
- print('m3.group():',m3.group())
- print('m3.start():',m3.start())
- print('m3.end():',m3.end())
- print('m3.span():',m3.span())
- pattern2 = re.compile(r'([a-z]+)([a-z])',re.I) #re.I 表示忽略大小写
- mm = pattern2.match('Hello World Wide Web')
- print('mm:',mm)
- print('mm.group(0):',mm.group(0))
- print('mm.span(0):',mm.span(0))
- print('mm.group(1):',mm.group(1))
- print('mm.span(1):',mm.span(1))
- print('mm.group(2):',mm.group(2))
- print('mm.span(2):',mm.span(2))
- print('mm.groups():',mm.groups())
- # print('mm.group(3):',mm.group(3))
- '''
- findall
- '''
- # 在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。
- # 注意: match 和 search 是匹配一次 findall 匹配所有。
- # 语法格式为:
- # findall(string[, pos[, endpos]])
- pattern3 = re.compile(r'\d+')
- result1 = pattern3.findall('runoob 123 google 456')
- result2 = pattern3.findall('run88oob123google456',0,10)
- print('result1:',result1)
- print('result2:',result2)
- '''
- re.finditer:找到正则表达式所匹配的所有子串,并把他们作为一个迭代器返回
- '''
- it = re.finditer(r'\d+','qaz12edc34edc4rfv56')
- for match in it:
- print(match.group())
- '''
- re.split
- '''
- print('1:',re.split('\W+','runoob,runoob,runoob.'))
- print('2:',re.split('(\W+)','runoob,runoob,runoob.'))
- print('',re.split('\W+','runoob,runoob,runoob.',1))
- '''
- 正则表达式对象
- '''
- '''
- 正则表达式修饰符-可选标志
- '''
- '''
- 正则表达式模式
- '''
- '''
- 正则表达式实例
- '''
30-Python3 正则表达式的更多相关文章
- 详解 Python3 正则表达式(五)
上一篇:详解 Python3 正则表达式(四) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些注明和修改 ^_^ 非捕获组和命名 ...
- 详解 Python3 正则表达式(四)
上一篇:详解 Python3 正则表达式(三) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些注明和修改 ^_^ 更多强大的功能 ...
- 详解 Python3 正则表达式(三)
上一篇:详解 Python3 正则表达式(二) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 模块级别的函数 ...
- 详解 Python3 正则表达式(二)
上一篇:详解 Python3 正则表达式(一) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 使用正则表达式 ...
- 详解 Python3 正则表达式(一)
本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 正则表达式介绍 正则表达式(Regular expressio ...
- python025 Python3 正则表达式
Python3 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. ...
- Python3 正则表达式
字符串是编程时涉及到的最多的一种数据结构,对字符串进行操作的需求几乎无处不在.比如判断一个字符串是否是合法的Email地址,虽然可以编程提取@前后的子串,再分别判断是否是单词和域名,但这样做不但麻烦, ...
- python3 正则表达式学习笔记
re.match函数 re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none. ~匹配成功re.match方法返回一个匹配的对象,否则返回No ...
- Python3正则表达式
正则表达式是一个特殊的字符序列,他能帮助你方便的检查一个字符串是否与某种模式匹配. re.match函数 re.match尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,matc ...
- python3正则表达式总结
转自csdn,去这里看更多吧: https://blog.csdn.net/weixin_40136018/article/details/81183504 还有一个废话很多的详细系列,在这里:htt ...
随机推荐
- SPL标准库-数据结构
数据结构:栈 );] = ;] = ;var_dump($array); 来自为知笔记(Wiz)
- .NET Core开发日志——Runtime IDentifier
.NET Core对于传统.NET开发人员而言是既熟悉又陌生的新平台,所以有时遇上出乎意料的事情也纯属正常情况.这时只需点耐心,多查查资料,努力找到原因,也未尝不是件有意义的体验. 比如当建完一个最简 ...
- 体验 ASP.NET Core 集成测试三剑客:xUnit.net、TestServer、EF Core InMemory
这是昨天解决的一个问题,针对一个 web api 的客户端代理类写集成测试,既要测试 web api,又要测试 web api 客户端. 测试 web api,就要在运行测试时自动启动 web api ...
- 基于微服务架构、运行于容器中的.NET Core示例应用eShopOnContainers
eShopOnContainers 是 <.NET Microservices – Architecture for Containerized .NET Applications>这本微 ...
- python-----双色球实现(实例1)
#输出用户指定组数的双色球信息,其中一组信息 6个红色号码获取范围(1-33),1个蓝色号码获取范围(1-16),要求一组信息中红球内号码不可重复 import randomdef get_ball( ...
- c#加"\n\r"不换行,变成字符串
质检模块,本想将每个错误分行, 比如:lyrerrormess += lyrname + "图层" + "缺少" + xmlFieldName + " ...
- 数据在内存中的存储方式( Big Endian和Little Endian的区别 )(x86系列则采用little endian方式存储数据)
https://www.cnblogs.com/renyuan/archive/2013/05/26/3099766.html 1.故事的起源 “endian”这个词出自<格列佛游记>.小 ...
- [skill] vim 操作多个window
前言: 分辨率越来越高,屏幕越来越大,行最长80不变,屏幕利用空白越来越大. 开多个window吧! 开window的命令: 平行开一个window:split <//path/file> ...
- 抽屉之Tornado实战(9)--装饰器实现用户登录状态验证
当然今天讲的验证,不只Tornado会用,以后用到web框架都会用到,最常见的场景就是只有用户登陆了才能执行某些操作,所以在执行这些操作前要先做登陆状态的验证. 比如:点赞,发布,评论等需要验证,都需 ...
- 配置ASM以及创建恢复目录
本次配置ASM沿用了搭建RAC的环境配置,系统选用CENTOS6.8 首先本地配置YUM,安装GRID集群件所需要的RPM包 [root@rac01 Packages]# cd /etc/yum.re ...