国产探花免费观看_亚洲丰满少妇自慰呻吟_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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 营口市| 通化县| 兴文县| 凤山县| 化州市| 沙湾县| 深水埗区| 格尔木市| 东安县| 皮山县| 吉木萨尔县| 富锦市| 金平| 巴林左旗| 通化县| 富阳市| 饶阳县| 本溪市| 镇原县| 通城县| 根河市| 南宁市| 襄垣县| 怀化市| 合川市| 大同市| 万安县| 湖北省| 亳州市| 沭阳县| 阿拉善左旗| 大荔县| 那坡县| 广昌县| 乌拉特中旗| 邵武市| 房产| 阜城县| 广河县| 白山市| 彩票|