import re def mask_credit_card(text): pattern = r'\b(?:\d{4}[-\s]?){3}\d{4}\b' return re.sub(pattern, lambda m: '*' * 12 + m.group()[-4:], text) sample_text = "Credit card: 1234-5678-9012-3456 and 9876543210123456" masked_text = mask_credit_card(sample_text) print("Original:", sample_text) print("Masked:", masked_text)