python里使用正则的findall函数的实例详解

yipeiwu_com5年前Python基础

python里使用正则的findall函数的实例详解

在前面学习了正则的search()函数,这个函数可以找到一个匹配的字符串返回,但是想找到所有匹配的字符串返回,怎么办呢?其实得使用findall()函数。如下例子:

#python 3. 6 
#蔡军生  
#http://blog.csdn.net/caimouse/article/details/51749579 
# 
import re 
 
text = 'abbaaabbbbaaaaa' 
 
pattern = 'ab' 
 
for match in re.findall(pattern, text): 
  print('Found {!r}'.format(match)) 

结果输出如下:

Found 'ab'
Found 'ab'

在这里找到两个匹配的字符串输出。

如有疑问请留言或者到本站社区交流讨论,希望通过本文能帮助到大家,谢谢大家对本站的支持!

相关文章

Python 日期区间处理 (本周本月上周上月...)

工具类 class CalendarUtils: """ 日期工具类 """ @staticmethod def delta_day(delta=0):...

numpy中的meshgrid函数的使用

numpy官方文档meshgrid函数帮助文档https://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.ht...

Python中列表元素转为数字的方法分析

本文实例讲述了Python中列表元素转为数字的方法。分享给大家供大家参考,具体如下: 有一个数字字符的列表: numbers = ['1', '5', '10', '8'] 想...

Django中的ajax请求

Django中的ajax请求

需求:实现ajax请求,在界面上任意地方点击,可以成功传参。 创建项目如下所示: settings.py文件的设置,这次我们除了要注册app和设置templates文件夹的路径,还要多...

详解python读取image

python 读取image 在python中我们有两个库可以处理图像文件,scipy和matplotlib. 安装库 pip install matplotlib pillow s...