python里使用正则表达式的组嵌套实例详解


Posted in Python onOctober 24, 2017

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 相关文章推荐
python中使用urllib2伪造HTTP报头的2个方法
Jul 07 Python
Python中的hypot()方法使用简介
May 18 Python
python Selenium爬取内容并存储至MySQL数据库的实现代码
Mar 16 Python
python实现随机梯度下降(SGD)
Mar 24 Python
pandas DataFrame数据转为list的方法
Apr 11 Python
pycharm中成功运行图片的配置教程
Oct 28 Python
python pygame模块编写飞机大战
Nov 20 Python
python 采用paramiko 远程执行命令及报错解决
Oct 21 Python
Windows下PyCharm2018.3.2 安装教程(图文详解)
Oct 24 Python
python FTP批量下载/删除/上传实例
Dec 22 Python
Python ADF 单位根检验 如何查看结果的实现
Jun 03 Python
python实现简单倒计时功能
Apr 21 Python
Scrapy的简单使用教程
Oct 24 #Python
详解python里使用正则表达式的分组命名方式
Oct 24 #Python
在python中使用正则表达式查找可嵌套字符串组
Oct 24 #Python
python爬虫之BeautifulSoup 使用select方法详解
Oct 23 #Python
浅谈python中copy和deepcopy中的区别
Oct 23 #Python
python的构建工具setup.py的方法使用示例
Oct 23 #Python
python使用pyqt写带界面工具的示例代码
Oct 23 #Python
You might like
PHP得到mssql的存储过程的输出参数功能实现
2012/11/23 PHP
ThinkPHP入库出现两次反斜线转义及数据库类转义的解决方法
2014/11/04 PHP
老版本PHP转义Json里的特殊字符的函数
2015/06/08 PHP
CodeIgniter整合Smarty的方法详解
2017/08/25 PHP
js实现简单模态窗口,背景灰显
2008/11/14 Javascript
jQuery 源代码显示控件 (Ajax加载方式).
2009/05/18 Javascript
JavaScript 事件对象的实现
2009/07/13 Javascript
jQuery查询数据返回object和字符串影响原因是什么
2013/08/09 Javascript
深入理解requestAnimationFrame的动画循环
2016/09/20 Javascript
Bootstrap常用组件学习(整理)
2017/03/24 Javascript
Vue2.0基于vue-cli+webpack同级组件之间的通信教程(推荐)
2017/09/14 Javascript
谈谈JS中的!!
2017/12/07 Javascript
图片文字识别(OCR)插件Ocrad.js教程
2018/11/26 Javascript
vue实现的组件兄弟间通信功能示例
2018/12/04 Javascript
详细讲解用Python发送SMTP邮件的教程
2015/04/29 Python
Python简单实现enum功能的方法
2016/04/25 Python
Python变量和数据类型详解
2017/02/15 Python
Python+threading模块对单个接口进行并发测试
2019/06/25 Python
python flask搭建web应用教程
2019/11/19 Python
运行Python编写的程序方法实例
2020/10/21 Python
python实现文件+参数发送request的实例代码
2021/01/05 Python
全网最详细的PyCharm+Anaconda的安装过程图解
2021/01/25 Python
matplotlib之属性组合包(cycler)的使用
2021/02/24 Python
俄罗斯第一家篮球店:StreetBall
2020/07/30 全球购物
Linux操作面试题
2012/05/16 面试题
家具厂厂长岗位职责
2014/01/01 职场文书
企业道德讲堂实施方案
2014/03/19 职场文书
财产公证书格式
2014/04/10 职场文书
法制演讲稿
2014/09/10 职场文书
机关党员三严三实心得体会
2014/10/13 职场文书
万能检讨书2000字
2014/10/17 职场文书
电影地道战观后感
2015/06/04 职场文书
小学数学教师研修感悟
2015/11/18 职场文书
导游词之江西赣州
2019/10/15 职场文书
Java 实现限流器处理Rest接口请求详解流程
2021/11/02 Java/Android
SQL Server2019数据库备份与还原脚本,数据库可批量备份
2021/11/20 SQL Server