关于re.finditer只有一个参数的疑问

我正在学习流畅的python这本书,书里有串代码

import sys
import re

WORD_RE = re.compile(r'w+')

index = {}
with open(sys.argv[1],encoding = 'utf-8') as fp:

for line_no,line in enumerate(fp,1):
    **for match in WORD_RE.finditer(line):**
        word = match.group()
        

我加粗的这段代码,用到的finditer()方法只传入了一个参数,得到的是什么,pattern是有默认值吗?

阅读 3.2k
3 个回答
WORD_RE = re.compile(r'w+')
WORD_RE.finditer(line)

你用的是compile后pattern对象的finditer方法
上面两行相当于re.finditer(r'w+', line)
pattern没有默认值,返回的是一个match对象迭代器。

看一下官方文档

re.finditer(pattern, string, flags=0)
Return an iterator yielding match objects over all non-overlapping matches for the RE pattern in string. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result.

Changed in version 3.7: Non-empty matches can now start just after a previous empty match.

再看一下re.findall

re.findall(pattern, string, flags=0)
Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result.

Changed in version 3.7: Non-empty matches can now start just after a previous empty match.

所以,这两者的功能是一样的,都是返回某个字符串中的全部匹配结果。不同之处在于re.findall返回一个列表,而re.finditer返回一个迭代器。

WORD_RE.finditer实际上是Pattern.finditer方法,WORD_RE是一个Pattern对象(re.compile返回一个Pattern对象),然后在这个对象上调用finditer方法。Pattern对象的方法和re模块的方法几乎是一一对应的。Pattern.finditer方法和re.finditer方法的唯一区别就是由于pattern已知,无需再传递pattern,只需要一个位置参数string就可以了。

新手上路,请多包涵

finditer返回迭代器,那个参数好像是默认的。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题