python里使用正則表達(dá)式的組嵌套實(shí)例詳解
python里使用正則表達(dá)式的組嵌套實(shí)例詳解
由于組本身是一個(gè)完整的正則表達(dá)式,所以可以將組嵌套在其他組中,以構(gòu)建更復(fù)雜的表達(dá)式。下面的例子,就是進(jìn)行組嵌套的例子:
#python 3.6 #蔡軍生 #http://blog.csdn.net/caimouse/article/details/51749579 # import re def test_patterns(text, patterns): """Given source text and a list of patterns, look for matches for each pattern within the text and print them to stdout. """ # Look for each pattern in the text and print the results for pattern, desc in patterns: print('{!r} ({})\n'.format(pattern, desc)) print(' {!r}'.format(text)) for match in re.finditer(pattern, text): s = match.start() e = match.end() prefix = ' ' * (s) print( ' {}{!r}{} '.format(prefix, text[s:e], ' ' * (len(text) - e)), end=' ', ) print(match.groups()) if match.groupdict(): print('{}{}'.format( ' ' * (len(text) - s), match.groupdict()), ) print() return
例子:
#python 3.6 #蔡軍生 #http://blog.csdn.net/caimouse/article/details/51749579 # from re_test_patterns_groups import test_patterns test_patterns( 'abbaabbba', [(r'a((a*)(b*))', 'a followed by 0-n a and 0-n b')], )
結(jié)果輸出如下:
'a((a*)(b*))' (a followed by 0-n a and 0-n b) 'abbaabbba' 'abb' ('bb', '', 'bb') 'aabbb' ('abbb', 'a', 'bbb') 'a' ('', '', '')
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
springboot aop方式實(shí)現(xiàn)接口入?yún)⑿r?yàn)的示例代碼
在實(shí)際開(kāi)發(fā)項(xiàng)目中,我們常常需要對(duì)接口入?yún)⑦M(jìn)行校驗(yàn),本文主要介紹了springboot aop方式實(shí)現(xiàn)接口入?yún)⑿r?yàn)的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08Python?jieba庫(kù)文本處理詞性標(biāo)注和關(guān)鍵詞提取進(jìn)行文本情感分析
這篇文章主要為大家介紹了Python使用中文文本處理利器jieba庫(kù)中的詞性標(biāo)注和關(guān)鍵詞提取功能進(jìn)行文本情感分析實(shí)例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Python實(shí)現(xiàn)常見(jiàn)數(shù)據(jù)格式轉(zhuǎn)換的方法詳解
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)常見(jiàn)數(shù)據(jù)格式轉(zhuǎn)換的方法,主要是xml_to_csv和csv_to_tfrecord,感興趣的小伙伴可以了解一下2022-09-09利用Python將每日一句定時(shí)推送至微信的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于利用Python將每日一句定時(shí)推送至微信的實(shí)現(xiàn)方法,文中通過(guò)示例代碼將實(shí)現(xiàn)的步驟一步步介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08