詳解python/272134.html">python里使用正則表達(dá)式的全匹配功能
python中很多匹配,比如搜索任意位置的search()函數(shù),搜索邊界的match()函數(shù),現(xiàn)在還需要學(xué)習(xí)一個全匹配函數(shù),就是搜索的字符與內(nèi)容全部匹配,它就是fullmatch()函數(shù)。
例子如下:
#python 3.6#蔡軍生 #http://blog.csdn.net/caimouse/article/details/51749579#import retext = '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 : isSearch : <_sre.SRE_Match object; span=(2, 4), match='is'>Full match : NoneText : isFull match : <_sre.SRE_Match object; span=(0, 2), match='is'>Text : issFull match : None
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答
圖片精選