python中的正则表达式的compile,match,search,sub的使用
来源:互联网 发布:交通数据百度百科 编辑:程序博客网 时间:2024/03/29 10:28
python中的正则表达式的compile,match,search,sub的使用
compile只会从开头哦你匹配,如果不是开头的字符串,那么将不会匹配成功,而search可以匹配任意位置的字符串,会对整个匹配对象进行扫描,sub方法类似于字符串的replace方法,sub是对所有匹配到的内容进行替换,而不仅仅是替换第一个匹配到的对象。
<pre style="font-family: 宋体; font-size: 13.5pt; background-color: rgb(255, 255, 255);">
#-*- coding:utf-8 -*-import restr = 'python is my love'a = re.compile('py')#compile只会从开头匹配,如果不是开头的字符串,那么将不会匹配成功b = a.match(str)#指定被匹配的对象print u'被匹配的对象:',b.stringprint u'匹配上的字符串位置索引:',b.span()print u'需要匹配的字符串:',b.group()print '-------------------------------'c = re.search('love',str)#search可以匹配任意位置的字符串,会对整个匹配对象进行扫描print u'被匹配的对象:',c.stringprint u'匹配上的字符串位置索引:',c.span()print u'需要匹配的字符串:',c.group()print '-------------------------------'d = re.sub('love','world',str)#sub方法会对匹配到的内容进行替换(注意:是替换所有匹配到的内容),类似字符串的replace方法print d
结果:
被匹配的对象: python is my love匹配上的字符串位置索引: (0, 2)需要匹配的字符串: py-------------------------------被匹配的对象: python is my love匹配上的字符串位置索引: (13, 17)需要匹配的字符串: love-------------------------------python is my world
0 0
- python中的正则表达式的compile,match,search,sub的使用
- python正则表达式中的match 和 search
- Python正则表达式中的 compile,search,group,groups 函数的简单说明
- Python正则表达式中的 compile,search,group,groups 函数的简单说明
- Python正则表达式中的 compile,search,group,groups 函数的简单说明
- python正则表达式函数match()和search()的区别
- python正则表达式模块,re.sub的函数使用
- python中re.sub的使用 正则表达式
- Python正则match()和search()的理解
- Learning Python 008 正则表达式-005 compile模板的使用
- python 正则表达式精华 re.match与re.search的区别
- python正则表达式模块re中search和match方法的区别
- 正则表达式之match search findall finditer的用法
- Python 正则表达式的使用(re findall finditer match search)- 千月的python linux 系统管理指南学习笔记(13)
- python3正则表达式简单使用、search()、match()等函数的区别
- python3正则表达式简单使用、search()、match()等函数的区别
- Python 正则表达式 sub
- python re.search 和 re.match 正则表达式
- 【Java基础提高】深入分析String(四)
- Distributed Systems-Paxos
- 颜色空间
- Tomcat日志系统详解
- Scala之ListBuffer、ArrayBuffer、Queue及Stack介绍
- python中的正则表达式的compile,match,search,sub的使用
- iOS 获取应用当前版本号
- HTML-CSS基本特性
- 第九周 项目一 猴子选大王(数组版)
- day04 循环 随机值 break和continue 循环嵌套 while 循环 do while
- SSH应用之BBS之路-0、写在最前
- DFA和NFA的区别
- User Base协同过滤的推荐系统,python实现
- HDOJ 2034 人见人爱A - B