您好,登錄后才能下訂單哦!
這篇文章主要介紹“python正則表達(dá)式函數(shù)match()和search()有哪些區(qū)別”,在日常操作中,相信很多人在python正則表達(dá)式函數(shù)match()和search()有哪些區(qū)別問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”python正則表達(dá)式函數(shù)match()和search()有哪些區(qū)別”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
match()函數(shù)只檢測(cè)RE是不是在string的開始位置匹配, search()會(huì)掃描整個(gè)string查找匹配, 也就是說match()只有在0位置匹配成功的話才有返回,如果不是開始位置匹配成功的話,match()就返回none
例如:
#! /usr/bin/env python # -*- coding=utf-8 -*- import re text= 'pythontab' m= re.match(r"\w+", text) if m: print m.group(0) else: print 'not match'
結(jié)果是:pythontab
而:
#! /usr/bin/env python # -*- coding=utf-8 -*- # import re text= '@pythontab' m= re.match(r"\w+", text) if m: print m.group(0) else: print 'not match'
結(jié)果是:not match
search()會(huì)掃描整個(gè)字符串并返回第一個(gè)成功的匹配
例如:
#! /usr/bin/env python # -*- coding=utf-8 -*- # import re text= 'pythontab' m= re.search(r"\w+", text) if m: print m.group(0) else: print 'not match'
結(jié)果是:pythontab
那這樣呢:
#! /usr/bin/env python # -*- coding=utf-8 -*- # import re text= '@pythontab' m= re.search(r"\w+", text) if m: print m.group(0) else: print 'not match'
結(jié)果是:pythontab
到此,關(guān)于“python正則表達(dá)式函數(shù)match()和search()有哪些區(qū)別”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。