Different behavior between re.finditer and re.findall

前端 未结 4 384
逝去的感伤
逝去的感伤 2021-01-31 04:59

I am using the following code:

CARRIS_REGEX=r\'(\\d+)([\\s\\w\\.\\-]+)(\\d+:\\d+)(\\d+m)         


        
4条回答
  •  被撕碎了的回忆
    2021-01-31 05:50

    You can't make them behave the same way, because they're different. If you really want to create a list of results from finditer, then you could use a list comprehension:

    >>> [match for match in pattern.finditer(mailbody)]
    [...]
    

    In general, use a for loop to access the matches returned by re.finditer:

    >>> for match in pattern.finditer(mailbody):
    ...     ...
    

提交回复
热议问题