import re
pattern = r"^.*\b(?:cart|order)\b.*|(\b\d{4,12}\b)"
text1 = 'the cart number is 1234 and 4567 that it!'
text2 = "order 12345"
text3 = "credit card is 0000 4567 and 3456"
text4 = "i got 245 dollar"
text5 = "the 4567 and 2345 "
strings = [
text1,
text2,
text3,
text4,
text5
]
for string in strings:
print([s.span() for s in re.finditer(pattern, string, re.M) if s])
aW1wb3J0IHJlIApwYXR0ZXJuID0gciJeLipcYig/OmNhcnR8b3JkZXIpXGIuKnwoXGJcZHs0LDEyfVxiKSIKCnRleHQxID0gJ3RoZSBjYXJ0IG51bWJlciBpcyAxMjM0IGFuZCA0NTY3IHRoYXQgaXQhJwp0ZXh0MiA9ICJvcmRlciAxMjM0NSIKdGV4dDMgPSAiY3JlZGl0IGNhcmQgaXMgMDAwMCA0NTY3IGFuZCAzNDU2Igp0ZXh0NCA9ICJpIGdvdCAyNDUgZG9sbGFyIgp0ZXh0NSA9ICJ0aGUgNDU2NyBhbmQgMjM0NSAiCgpzdHJpbmdzID0gWwogICAgdGV4dDEsCiAgICB0ZXh0MiwKICAgIHRleHQzLAogICAgdGV4dDQsCiAgICB0ZXh0NQpdCgpmb3Igc3RyaW5nIGluIHN0cmluZ3M6CiAgICBwcmludChbcy5zcGFuKCkgZm9yIHMgaW4gcmUuZmluZGl0ZXIocGF0dGVybiwgc3RyaW5nLCByZS5NKSBpZiBzXSkKCg==
[(0, 41)]
[(0, 11)]
[(15, 19), (20, 24), (29, 33)]
[]
[(4, 8), (13, 17)]