Python面试题----Python 的re模块中match、search、findall、finditer的区别

版权声明:本文为博主原创文章,未经允许不得转载。 https://blog.csdn.net/wem603947175/article/details/83140767

请简要说明Python 的re模块中match、search、findall、finditer的区别


re是Python中用于正则表达式相关处理的类,这四个方法都是用于匹配字符串的,具体区别如下:

match

匹配string 开头,成功返回Match object, 失败返回None,只匹配一个。

search

在string中进行搜索,成功返回Match object, 失败返回None, 只匹配一个。

findall

在string中查找所有 匹配成功的组, 即用括号括起来的部分。返回list对象,每个list item是由每个匹配的所有组组成的list。

finditer

在string中查找所有 匹配成功的字符串, 返回iterator,每个item是一个Match object。

借鉴—亦游的原文

猜你喜欢

转载自blog.csdn.net/wem603947175/article/details/83140767