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'

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

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

02-02 06:15
查看更多