詳解python里使用正則表達式的全匹配功能
更新時間:2017年10月19日 08:40:53 作者:caimouse
這篇文章主要介紹了詳解python里使用正則表達式的全匹配功能的相關資料,希望通過本文能幫助到大家,需要的朋友可以參考下
詳解python里使用正則表達式的全匹配功能
python中很多匹配,比如搜索任意位置的search()函數(shù),搜索邊界的match()函數(shù),現(xiàn)在還需要學習一個全匹配函數(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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
Python利用contextvars實現(xiàn)管理上下文變量
Python?在?3.7?的時候引入了一個模塊:contextvars,從名字上很容易看出它指的是上下文變量。所以本文就來和大家詳細講講如何使用contextvars實現(xiàn)管理上下文變量,需要的可以參考一下2022-07-07
Python Numpy數(shù)組擴展repeat和tile使用實例解析
這篇文章主要介紹了Python Numpy數(shù)組擴展repeat和tile使用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12

