Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

Currently, I'm using the below code:

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

pattern = re.compile(CARRIS_REGEX, re.UNICODE)

matches = pattern.finditer(mailbody)

findall = pattern.findall(mailbody)

Yet, finditer and findall are finding various things. Findall surely discovers all the matches in the given string. Yet, finditer just finds the first, returning an iterator with just a single component. 

How might I make finditer and findall act a similar way?

1 Answer

0 votes
by (26.4k points)

I can't repeat this here. Have attempted it with both Python 2.7 and 3.1. 

One distinction among finditer and findall is that the previous returns regex coordinate items while different returns a tuple of the coordinated catching gatherings (or the whole match if there are no catching gatherings). 

so,

import re

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

pattern = re.compile(CARRIS_REGEX, re.UNICODE)

mailbody = open("test.txt").read()

for match in pattern.finditer(mailbody):

    print(match)

print()

for match in pattern.findall(mailbody):

    print(match)

Which prints,

<_sre.SRE_Match object at 0x00A63758>

<_sre.SRE_Match object at 0x00A63F98>

<_sre.SRE_Match object at 0x00A63758>

<_sre.SRE_Match object at 0x00A63F98>

<_sre.SRE_Match object at 0x00A63758>

<_sre.SRE_Match object at 0x00A63F98>

<_sre.SRE_Match object at 0x00A63758>

<_sre.SRE_Match object at 0x00A63F98>

('790', 'PR. REAL', '21:06', '04m')

('758', 'PORTAS BENFICA', '21:10', '09m')

('790', 'PR. REAL', '21:14', '13m')

('758', 'PORTAS BENFICA', '21:21', '19m')

('790', 'PR. REAL', '21:29', '28m')

('758', 'PORTAS BENFICA', '21:38', '36m')

('758', 'SETE RIOS', '21:49', '47m')

('758', 'SETE RIOS', '22:09', '68m')

In the event thatyou need a similar yield from finditer as you're getting from findall, you need

for match in pattern.finditer(mailbody):

    print(tuple(match.groups()))

Looking for python tutorial course? Join python certification course and get certified.

Watch the below video tutorial to gain more knowledge on python...

Related questions

0 votes
1 answer
asked Oct 4, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Oct 3, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...