python的re模塊應(yīng)用實(shí)例
本文實(shí)例講述了python的re模塊應(yīng)用。是非常重要的應(yīng)用技巧。分享給大家供大家參考。
具體方法如下:
import re # match_object = re.match('foo','foo') if match_object is not None: print type(match_object) print match_object.group() # match_object = re.match('foo','fooabv') if match_object is not None: print match_object.group() #match從頭開始匹配 match_object = re.match('foo','afooabv') if match_object is not None: print match_object.group() else: print 'not match' #利用面向?qū)ο蟮奶攸c(diǎn),一行完成 print re.match('love','lovesomebody is a happy thing').group() #與match的區(qū)別:match從頭開始匹配,search是查找 match_object = re.search('foo','afooabv') if match_object is not None: print match_object.group() else: print 'not match' #|的使用 bt = 'bat|bit|bot' match_object = re.match(bt,'batsdf') if match_object is not None: print "|...|" + match_object.group()#會匹配成功 else: print 'not match' bt = 'bat|bit|bot' match_object = re.search(bt,'aabatsdf') if match_object is not None: print "|search|" + match_object.group()#會匹配成功,如果用match就不會匹配成功 else: print 'not match'
本文實(shí)例測試環(huán)境為Python2.7.6
運(yùn)行結(jié)果如下:
<type '_sre.SRE_Match'> foo foo not match love foo |...|bat |search|bat
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
python3 requests庫實(shí)現(xiàn)多圖片爬取教程
今天小編就為大家分享一篇python3 requests庫實(shí)現(xiàn)多圖片爬取教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python基于回溯法子集樹模板解決取物搭配問題實(shí)例
這篇文章主要介紹了Python基于回溯法子集樹模板解決取物搭配問題,簡單描述了搭配問題并結(jié)合實(shí)例形式分析了Python使用回溯法子集樹模板解決取物搭配問題的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-09-09Python實(shí)現(xiàn)解析Bit Torrent種子文件內(nèi)容的方法
這篇文章主要介紹了Python實(shí)現(xiàn)解析Bit Torrent種子文件內(nèi)容的方法,結(jié)合實(shí)例形式分析了Python針對Torrent文件的讀取與解析相關(guān)操作技巧與注意事項,需要的朋友可以參考下2017-08-08python機(jī)器學(xué)習(xí)之決策樹分類詳解
這篇文章主要介紹了python機(jī)器學(xué)習(xí)之決策樹分類,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12python實(shí)現(xiàn)馬丁策略的實(shí)例詳解
這篇文章主要介紹了python實(shí)現(xiàn)馬丁策略的實(shí)例詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01python去除字符串中的空格、特殊字符和指定字符的三種方法
本文主要介紹了python去除字符串中的空格、特殊字符和指定字符的三種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02