国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python里使用正則表達式的組嵌套實例詳解

2020-01-04 16:39:00
字體:
來源:轉載
供稿:網友

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' ('', '', '')

 如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 刚察县| 迭部县| 宜良县| 梁河县| 宜春市| 苍南县| 延寿县| 山阴县| 奉新县| 墨江| 运城市| 枣阳市| 长丰县| 张家港市| 高青县| 抚松县| 北宁市| 唐山市| 高要市| 含山县| 榆林市| 广德县| 那坡县| 云梦县| 且末县| 兴文县| 娱乐| 苍梧县| 沙湾县| 海兴县| 桂阳县| 砚山县| 乌什县| 陈巴尔虎旗| 宜章县| 临武县| 大庆市| 邯郸县| 广饶县| 三亚市| 满城县|