python正则表达式match和search用法实例


Posted in Python onMarch 26, 2015

本文实例讲述了python正则表达式match和search用法。分享给大家供大家参考。具体分析如下:

python提供了2中主要的正则表达式操作:re.match 和 re.search。

match :只从字符串的开始与正则表达式匹配,匹配成功返回matchobject,否则返回none;

search :将字符串的所有字串尝试与正则表达式匹配,如果所有的字串都没有匹配成功,返回none,否则返回matchobject;(re.search相当于perl中的默认行为)

import re
def testsearchandmatch():
 s1="helloworld, i am 30 !"
 w1 = "world"
 m1 = re.search(w1, s1)
 if m1:
 print("find : %s" % m1.group())
 if re.match(w1, s1) == none:
 print("cannot match")
 w2 = "helloworld"
 m2 = re.match(w2, s1)
 if m2:
 print("match : %s" % m2.group())
testsearchandmatch()
#find : world
#cannot match
#match : helloworld
Python 相关文章推荐
Python三元运算实现方法
Jan 12 Python
python协程用法实例分析
Jun 04 Python
Python引用模块和查找模块路径
Mar 17 Python
python基础教程之五种数据类型详解
Jan 12 Python
详解python中executemany和序列的使用方法
Aug 12 Python
Python实现PS滤镜的万花筒效果示例
Jan 23 Python
对python3 中方法各种参数和返回值详解
Dec 15 Python
Python中如何导入类示例详解
Apr 17 Python
十个Python练手的实战项目,学会这些Python就基本没问题了(推荐)
Apr 26 Python
Python正则表达式匹配数字和小数的方法
Jul 03 Python
Python的垃圾回收机制详解
Aug 28 Python
Python爬虫实现模拟点击动态页面
Mar 05 Python
python根据开头和结尾字符串获取中间字符串的方法
Mar 26 #Python
pymongo实现控制mongodb中数字字段做加法的方法
Mar 26 #Python
python使用PythonMagick将jpg图片转换成ico图片的方法
Mar 26 #Python
python使用正则表达式分析网页中的图片并进行替换的方法
Mar 26 #Python
python轻松实现代码编码格式转换
Mar 26 #Python
使用python实现正则匹配检索远端FTP目录下的文件
Mar 25 #Python
python通过wxPython打开一个音频文件并播放的方法
Mar 25 #Python
You might like
中英文字符串翻转函数
2008/12/09 PHP
PHP实现递归目录的5种方法
2016/10/27 PHP
浅谈JavaScript实现面向对象中的类
2014/12/09 Javascript
javascript实现避免页面按钮重复提交
2015/01/08 Javascript
js使用onmousemove和onmouseout获取鼠标坐标的方法
2015/03/31 Javascript
原生js实现查询天气小应用
2016/12/09 Javascript
基于jquery实现左右上下移动效果
2018/05/02 jQuery
微信小程序中使用ECharts 异步加载数据实现图表功能
2018/07/13 Javascript
微信小程序实现多行文字超出部分省略号显示功能
2019/10/23 Javascript
Python中static相关知识小结
2018/01/02 Python
Python数据分析之双色球中蓝红球分析统计示例
2018/02/03 Python
Python3匿名函数lambda介绍与使用示例
2019/05/18 Python
利用Python库Scapy解析pcap文件的方法
2019/07/23 Python
在python image 中安装中文字体的实现方法
2019/08/22 Python
分享8点超级有用的Python编程建议(推荐)
2019/10/13 Python
Python列表解析操作实例总结
2020/02/26 Python
Python3 用什么IDE开发工具比较好
2020/11/28 Python
详解CSS3媒体查询响应式布局bootstrap 框架原理实战(推荐)
2020/11/16 HTML / CSS
Dr. Martens马汀博士官网:马丁靴始祖品牌
2016/10/15 全球购物
美国Randolph太阳镜官网:美国制造的飞行员太阳镜和射击眼镜
2018/06/15 全球购物
英国护发和美妆在线商店:Klip Shop
2019/03/24 全球购物
物流司机岗位职责
2013/12/28 职场文书
给老婆的婚前保证书
2014/02/01 职场文书
环保倡议书100字
2014/05/15 职场文书
测控技术自荐信
2014/06/05 职场文书
圣诞节活动策划方案
2014/06/09 职场文书
财务会计实训报告
2014/11/05 职场文书
催款函范文
2015/06/24 职场文书
《我们的民族小学》教学反思
2016/02/19 职场文书
iPhone13再次曝光
2021/04/15 数码科技
详解JavaScript中的执行上下文及调用堆栈
2021/04/29 Javascript
使用golang编写一个并发工作队列
2021/05/08 Golang
Java并发编程之详解CyclicBarrier线程同步
2021/06/23 Java/Android
SpringCloud的JPA连接PostgreSql的教程
2021/06/26 Java/Android
Python语言内置数据类型
2022/02/24 Python
PC版《死亡搁浅导剪版》现已发售 展开全新的探险
2022/04/03 其他游戏