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

