fork download
  1. import re
  2.  
  3. regex = r"(?<!\S)\d{1,4}(?:-\d{1,4})+(?!\S)"
  4.  
  5. test_str = ("In the US 555-0198 and 1-206-5705-0100 are examples fictitious numbers.\n"
  6. " In the UK, 044-113-496-1834 is a fictitious number.\n"
  7. " In Ireland, the number 353-020-917-1234 is fictitious.\n"
  8. " And in Australia, 061-970-654-321 is a fictitious number.\n"
  9. " 311 is a joke.")
  10.  
  11. result = re.sub(
  12. regex,
  13. lambda x: re.sub(r'\d', '0', x.group())
  14. if len(x.group().replace('-', '')) in (7, 11, 12, 13) else x.group(), test_str
  15. )
  16.  
  17. print(result)
Success #stdin #stdout 0.03s 9688KB
stdin
Standard input is empty
stdout
In the US 000-0000 and 0-000-0000-0000 are examples fictitious numbers.
            In the UK, 000-000-000-0000 is a fictitious number.
            In Ireland, the number 000-000-000-0000 is fictitious.
            And in Australia, 000-000-000-000 is a fictitious number.
            311 is a joke.