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 相关文章推荐
在 Django/Flask 开发服务器上使用 HTTPS
Jul 03 Python
python中列表元素连接方法join用法实例
Apr 07 Python
Windows下Python2与Python3两个版本共存的方法详解
Feb 12 Python
Python 快速实现CLI 应用程序的脚手架
Dec 05 Python
python主线程捕获子线程的方法
Jun 17 Python
python实现指定文件夹下的指定文件移动到指定位置
Sep 17 Python
Python3.5文件修改操作实例分析
May 01 Python
Python检测数据类型的方法总结
May 20 Python
pyQt5实时刷新界面的示例
Jun 25 Python
pytorch进行上采样的种类实例
Feb 18 Python
django queryset相加和筛选教程
May 18 Python
如何利用python创作字符画
Jun 25 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
PHP中常用的输出函数总结
2014/09/22 PHP
PHPStrom 新建FTP项目以及在线操作教程
2016/10/16 PHP
thinkPHP数据库增删改查操作方法实例详解
2016/12/06 PHP
PHP安全之register_globals的on和off的区别
2020/07/23 PHP
JavaScript window.setTimeout() 的详细用法
2009/11/04 Javascript
jquery使用ColorBox弹出图片组浏览层实例演示
2013/03/14 Javascript
javascript中直接引用Microsoft的COM生成Word
2014/01/20 Javascript
javascript 中that的含义示例介绍
2014/05/14 Javascript
对比分析AngularJS中的$http.post与jQuery.post的区别
2015/02/27 Javascript
javascript清空table表格的方法
2015/05/14 Javascript
javascript实现点击单选按钮链接转向对应网址的方法
2015/08/12 Javascript
Jquery中巧用Ajax的beforeSend方法
2016/01/20 Javascript
微信小程序 图片等比例缩放(图片自适应屏幕)
2016/11/16 Javascript
jQuery的extend方法【三种】
2016/12/14 Javascript
javascript  数组排序与对象排序的实例
2017/07/17 Javascript
JS实现按钮颜色切换效果
2020/09/05 Javascript
vue cli4下环境变量和模式示例详解
2020/04/09 Javascript
vue配置多代理服务接口地址操作
2020/09/08 Javascript
[07:37]DOTA2-DPC中国联赛2月2日Recap集锦
2021/03/11 DOTA
Python的高阶函数用法实例分析
2019/04/11 Python
详解python实现小波变换的一个简单例子
2019/07/18 Python
python如何绘制疫情图
2020/09/16 Python
基于Python爬取京东双十一商品价格曲线
2020/10/23 Python
css3和jquery实现自定义checkbox和radiobox组件
2014/04/22 HTML / CSS
CSS3移动端vw+rem不依赖JS实现响应式布局的方法
2019/01/23 HTML / CSS
大都会艺术博物馆商店:The Met Store
2018/06/22 全球购物
戴尔新加坡官网:Dell Singapore
2020/12/13 全球购物
应届生服务员求职信
2013/10/31 职场文书
公司庆典邀请函范文
2014/01/13 职场文书
团队精神口号
2014/06/06 职场文书
毕业典礼邀请函
2015/01/31 职场文书
小学班主任个人总结
2015/03/03 职场文书
2015年幼儿园班主任个人工作总结
2015/10/22 职场文书
2016年校园重阳节广播稿
2015/12/18 职场文书
Python+OpenCV实现在图像上绘制矩形
2022/03/21 Python
向Spring IOC 容器动态注册bean实现方式
2022/07/15 Java/Android