Is it possible to get matchObject using re.finditer() rather than re.findall()?

Viewed 26

I am matching number that does not start with words cart or order. And also number that does not end with dollar. Can I do this with finditer().

import re2 as re
text1 = ('the cart number is 1234 and 4567 that it!')
text2 = ("card number note down 12345 and cart number is 1234")
text3 = "credit card is 0000 1234 and 2345"
text4 = "the 4567 2345 "
text5 = "credit card id 1234 and 2345 i got 24558 dollar"
pattern = r"^.*\b(?:cart|order)\b.*|(\b\d[\s-]?\d{2,18}\b)(?! dollars?)"

pattern = r"^(?!.*\b(?:cart|order)).*(\b\d[\s-]?\d{2,12}\b)(?! dollars?)"

for s in re.findall(pattern, text1,re.M):
    if s:
        print(s)

Expected output:

text1  None

text2  None

text3  0000 
       1234 
       2345

text4  4567 
       2345

text5  1234
       2345

0 Answers
Related