mask_cards(text) replacing every 16-digit run (optionally grouped as 4×4 with spaces or hyphens) so only the last four digits survive: '** ** 1234'. Use re.sub with a function or backreference for the kept group.mask_cards('pay with 4242 4242 4242 4242 today')'pay with **** **** **** 4242 today'
Twelve digits masked, last four kept.
Pattern: \b(?:\d{4}[ -]?){3}(\d{4})\b.
Replacement: '**** **** **** \\1' or a lambda using m.group(1).