详解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-numpy-指数分布实例详解

如下所示: # Seed random number generator np.random.seed(42) # Compute mean no-hitter time: ta...

python实现人脸识别经典算法(一) 特征脸法

近来想要做一做人脸识别相关的内容,主要是想集成一个系统,看到opencv已经集成了三种性能较好的算法,但是还是想自己动手试一下,毕竟算法都比较初级。 操作环境:python2.7 第三方...

在Python中使用HTML模版的教程

在Python中使用HTML模版的教程

Web框架把我们从WSGI中拯救出来了。现在,我们只需要不断地编写函数,带上URL,就可以继续Web App的开发了。 但是,Web App不仅仅是处理逻辑,展示给用户的页面也非常重要。...

Python数学形态学实例分析

Python数学形态学实例分析

本文实例讲述了Python数学形态学。分享给大家供大家参考,具体如下: 一 原始随机图像 1、代码 import numpy as np import matplotlib.pypl...

Python堆排序原理与实现方法详解

Python堆排序原理与实现方法详解

本文实例讲述了Python堆排序原理与实现方法。分享给大家供大家参考,具体如下: 在这里要事先说明一下我也是新手,很多东西我了解不是很深入,写算法完全是锻炼自己逻辑能力同时顺带帮助读研的...