python里使用正則表達式的組嵌套實例詳解
更新時間:2017年10月24日 09:06:50 作者:caimouse
這篇文章主要介紹了python里使用正則表達式的組嵌套實例詳解的相關資料,希望通過本文能幫助到大家,需要的朋友可以參考下
python里使用正則表達式的組嵌套實例詳解
由于組本身是一個完整的正則表達式,所以可以將組嵌套在其他組中,以構建更復雜的表達式。下面的例子,就是進行組嵌套的例子:
#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')], )
結果輸出如下:
'a((a*)(b*))' (a followed by 0-n a and 0-n b) 'abbaabbba' 'abb' ('bb', '', 'bb') 'aabbb' ('abbb', 'a', 'bbb') 'a' ('', '', '')
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
springboot aop方式實現(xiàn)接口入?yún)⑿r灥氖纠a
在實際開發(fā)項目中,我們常常需要對接口入?yún)⑦M行校驗,本文主要介紹了springboot aop方式實現(xiàn)接口入?yún)⑿r灥氖纠a,具有一定的參考價值,感興趣的可以了解一下2023-08-08Python?jieba庫文本處理詞性標注和關鍵詞提取進行文本情感分析
這篇文章主要為大家介紹了Python使用中文文本處理利器jieba庫中的詞性標注和關鍵詞提取功能進行文本情感分析實例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12Python實現(xiàn)常見數(shù)據(jù)格式轉換的方法詳解
這篇文章主要為大家詳細介紹了Python實現(xiàn)常見數(shù)據(jù)格式轉換的方法,主要是xml_to_csv和csv_to_tfrecord,感興趣的小伙伴可以了解一下2022-09-09利用Python將每日一句定時推送至微信的實現(xiàn)方法
這篇文章主要給大家介紹了關于利用Python將每日一句定時推送至微信的實現(xiàn)方法,文中通過示例代碼將實現(xiàn)的步驟一步步介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧2018-08-08