简体   繁体   中英

print all matches of re.finditer (python)

I am reading a PDF file (on one script using PyPDF2) and on this one using (tika). In both, I have a problem with re.finditer . I'll have a line of code like this:

bank_pattern = '^.* (Bank|bank|BANK).*$'
bank = re.finditer(bank_pattern, text)
print('Here should be the bank name:')
print(bank)
print('')

for match in bank:
    print(match)

And I get following:

Here should be the bank name:
<callable_iterator object at 0x0000020BA86B4430>

Can someone help me understand why doesn't it show the matches? (I am trying to get the whole line where BANK, bank or Bank are mentioned - before and after the match)

PS read PDF part with banks:

Intermediary Bank (USD): censored,
New York, USA; SWIFT: censored 
Intermediary Bank (EUR): censored,
Frankfurt, Germany; SWIFT: censored

Thanks!

We can use re.findall here with the pattern ^.*\bbank\b.*$ :

inp = """Intermediary Bank (USD): censored,
New York, USA; SWIFT: censored 
Intermediary Bank (EUR): censored,
Frankfurt, Germany; SWIFT: censored"""

lines = re.findall(r'^.*\bbank\b.*$', inp, flags=re.I|re.M)
print(lines)

This prints:

['Intermediary Bank (USD): censored,', 'Intermediary Bank (EUR): censored,']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM