fork download
  1. import re
  2.  
  3. regex = r"\b[A-Za-z]{3,} [a-zA-Z\d]{1,2},? [a-zA-Z\d]{4}\b|\([a-zA-Z\d]+\)[a-zA-Z\d]{3}-[a-zA-Z\d]{4}\b"
  4. test_str = ("Jan 6, xxxx or (xxx)xxx-1234\n"
  5. "Jan 16, xxxx or (xxx)xxx-1234\n"
  6. "January 9 xxxx\n"
  7. "(xxx)xxx-1234")
  8. matches = re.sub(regex, lambda x: x.group().replace('x', '1'), test_str)
  9. print(matches)
Success #stdin #stdout 0.02s 9576KB
stdin
Standard input is empty
stdout
Jan 6, 1111 or (111)111-1234
Jan 16, 1111 or (111)111-1234
January 9 1111
(111)111-1234