详解python里使用正则表达式的全匹配功能


Posted in Python onOctober 19, 2017

详解python里使用正则表达式的全匹配功能

python中很多匹配,比如搜索任意位置的search()函数,搜索边界的match()函数,现在还需要学习一个全匹配函数,就是搜索的字符与内容全部匹配,它就是fullmatch()函数。

例子如下:

#python 3.6
#蔡军生 
#http://blog.csdn.net/caimouse/article/details/51749579
#
import re


text = 'This is some text -- with punctuation.'
pattern = 'is'


print('Text    :', text)
print('Pattern  :', pattern)


m = re.search(pattern, text)
print('Search   :', m)
s = re.fullmatch(pattern, text)
print('Full match :', s)




text = 'is'
print('Text    :', text)
s = re.fullmatch(pattern, text)
print('Full match :', s)


text = 'iss'
print('Text    :', text)
s = re.fullmatch(pattern, text)
print('Full match :', s)

结果输出如下:

Text    : This is some text -- with punctuation.
Pattern  : is
Search   : <_sre.SRE_Match object; span=(2, 4), match='is'>
Full match : None
Text    : is
Full match : <_sre.SRE_Match object; span=(0, 2), match='is'>
Text    : iss
Full match : None

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Python 相关文章推荐
python批量同步web服务器代码核心程序
Sep 01 Python
使用Python中的线程进行网络编程的入门教程
Apr 15 Python
python3+PyQt5图形项的自定义和交互 python3实现page Designer应用程序
Jul 20 Python
python批量识别图片指定区域文字内容
Apr 30 Python
华为2019校招笔试题之处理字符串(python版)
Jun 25 Python
python对数组进行排序,并输出排序后对应的索引值方式
Feb 28 Python
Pandas将列表(List)转换为数据框(Dataframe)
Apr 24 Python
pycharm 激活码及使用方式的详细教程
May 12 Python
Virtualenv 搭建 Py项目运行环境的教程详解
Jun 22 Python
浅析Python __name__ 是什么
Jul 07 Python
Python下划线5种含义代码实例解析
Jul 10 Python
Python中字符串对象语法分享
Feb 24 Python
python中logging库的使用总结
Oct 18 #Python
R vs. Python 数据分析中谁与争锋?
Oct 18 #Python
Ubuntu安装Jupyter Notebook教程
Oct 18 #Python
python 中的divmod数字处理函数浅析
Oct 17 #Python
Python中的id()函数指的什么
Oct 17 #Python
Python中int()函数的用法浅析
Oct 17 #Python
一文总结学习Python的14张思维导图
Oct 17 #Python
You might like
session在PHP大型web应用中的使用
2011/06/25 PHP
PHP和Mysqlweb应用开发核心技术 第1部分 Php基础-1 开始了解php
2011/07/03 PHP
PHP的命令行命令使用指南
2015/08/18 PHP
thinkPHP使用post方式查询时分页失效的解决方法
2015/12/09 PHP
laravel5 Eloquent 实现事务方式
2019/10/21 PHP
Javascript 学习笔记 错误处理
2009/07/30 Javascript
jQuery toggleClass应用实例(附效果图)
2014/04/06 Javascript
分享两个手机访问pc网站自动跳转手机端网站代码
2020/12/24 Javascript
jQuery中hover方法和toggle方法使用指南
2015/02/27 Javascript
jQuery使用unlock.js插件实现滑动解锁
2017/04/04 jQuery
vue axios同步请求解决方案
2017/09/29 Javascript
js实时监控文本框输入字数的实例代码
2018/01/18 Javascript
vue之浏览器存储方法封装实例
2018/03/15 Javascript
Promise.all中对于reject的处理方法
2018/08/01 Javascript
jQuery动态操作表单示例【基于table表格】
2018/12/06 jQuery
js实现图片放大并跟随鼠标移动特效
2019/01/18 Javascript
webpack常用构建优化策略小结
2019/11/21 Javascript
vue 清空input标签 中file的值操作
2020/07/21 Javascript
vue项目实现多语言切换的思路
2020/09/17 Javascript
python在linux系统下获取系统内存使用情况的方法
2015/05/11 Python
python 基础教程之Map使用方法
2017/01/17 Python
django模型层(model)进行建表、查询与删除的基础教程
2017/11/21 Python
Python标准库使用OrderedDict类的实例讲解
2019/02/14 Python
Python 单例设计模式用法实例分析
2019/09/23 Python
pandas factorize实现将字符串特征转化为数字特征
2019/12/19 Python
Python能做什么
2020/06/02 Python
浅谈python锁与死锁问题
2020/08/14 Python
迪梵英国官方网站:Darphin英国
2017/12/06 全球购物
在网络中有两台主机A和B,并通过路由器和其他交换设备连接起来,已经确认物理连接正确无误,怎么来测试这两台机器是否连通?如果不通,怎么来判断故障点?怎么排
2014/01/13 面试题
高三励志标语
2014/06/05 职场文书
售房协议书范本2014
2014/10/23 职场文书
小学科学教学计划
2015/01/21 职场文书
转让协议书
2015/01/27 职场文书
文员岗位职责范本
2015/04/16 职场文书
致我们终将逝去的青春观后感
2015/06/10 职场文书
Java Socket实现Redis客户端的详细说明
2021/05/26 Redis