脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python里使用正则的findall函数的实例详解

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

2020-12-12 00:18caimouse Python

这篇文章主要介绍了python里使用正则的findall函数的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
#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))

结果输出如下:

?
1
2
Found 'ab'
Found 'ab'

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

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

原文链接:http://blog.csdn.net/caimouse/article/details/78168969

延伸 · 阅读

精彩推荐