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

yipeiwu_com6年前Python基础

详解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的列表很重要,学习到后面你会发现使用的地方真的太多了。最近在写一些小项目时经常用到列表,有时其中的方法还会忘哎! 所以为了复习写下了这篇博客,大家也可以来学习一下,应该比较全...

Python使用Windows API创建窗口示例【基于win32gui模块】

Python使用Windows API创建窗口示例【基于win32gui模块】

本文实例讲述了Python使用Windows API创建窗口。分享给大家供大家参考,具体如下: 一、代码 # -*- coding:utf-8 -*- #! python3 impo...

Python深入学习之装饰器

装饰器(decorator)是一种高级Python语法。装饰器可以对一个函数、方法或者类进行加工。在Python中,我们有多种方法对函数和类进行加工,比如在Python闭包中,我们见到函...

Python中lambda的用法及其与def的区别解析

python中的lambda通常是用来在python中创建匿名函数的,而用def创建的方法是有名称的,除了从表面上的方法名不一样外,python中的lambda还有如下几点和def不一样...

如何更改 pandas dataframe 中两列的位置

如何更改 pandas dataframe 中两列的位置

如何更改 pandas dataframe 中两列的位置: 把其中的某列移到第一列的位置。 原来的 df 是: df = pd.read_csv('I:/Papers/consume...