詳解python里使用正則表達(dá)式的全匹配功能
詳解python里使用正則表達(dá)式的全匹配功能
python中很多匹配,比如搜索任意位置的search()函數(shù),搜索邊界的match()函數(shù),現(xiàn)在還需要學(xué)習(xí)一個(gè)全匹配函數(shù),就是搜索的字符與內(nèi)容全部匹配,它就是fullmatch()函數(shù)。
例子如下:
#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)
結(jié)果輸出如下:
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
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Python使用Scrapy保存控制臺(tái)信息到文本解析
這篇文章主要介紹了Python使用Scrapy保存控制臺(tái)信息到文本解析,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12
Python簡單實(shí)現(xiàn)gif動(dòng)圖倒放示例
這篇文章主要為大家介紹了Python簡單實(shí)現(xiàn)gif動(dòng)圖倒放的示例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python利用contextvars實(shí)現(xiàn)管理上下文變量
Python?在?3.7?的時(shí)候引入了一個(gè)模塊:contextvars,從名字上很容易看出它指的是上下文變量。所以本文就來和大家詳細(xì)講講如何使用contextvars實(shí)現(xiàn)管理上下文變量,需要的可以參考一下2022-07-07
Python Numpy數(shù)組擴(kuò)展repeat和tile使用實(shí)例解析
這篇文章主要介紹了Python Numpy數(shù)組擴(kuò)展repeat和tile使用實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12

