fork download
  1. import re
  2.  
  3. text = {'Forest': '123-456-7890', 'Johanna': '(987) 654-4321', 'Mom': '555.555.5555', 'Camille':'9988776655'}
  4.  
  5. regexPat = r'^(?:\d{3}-\d{3}-\d{4}|\(\d{3}\) \d{3}-\d{4}|\d{3}\.\d{3}\.\d{4}|\d{10})$'
  6.  
  7. print("Using 'pipes' to separate possible regex patterns")
  8.  
  9. phNum = re.compile(regexPat)
  10.  
  11. for k in text:
  12. mo = phNum.search(text[k])
  13. if mo:
  14. phone_num_text = "".join(c for c in mo.group() if c.isdigit())
  15. print(f"{k}'s area code: {phone_num_text[:3]}")
  16. print(f'Suffix: {phone_num_text[3:]}')
  17. print(f'Whole Number: {phone_num_text}')
  18.  
Success #stdin #stdout 0.03s 9432KB
stdin
Standard input is empty
stdout
Using 'pipes' to separate possible regex patterns
Forest's area code: 123
Suffix: 4567890
Whole Number: 1234567890
Johanna's area code: 987
Suffix: 6544321
Whole Number: 9876544321
Mom's area code: 555
Suffix: 5555555
Whole Number: 5555555555
Camille's area code: 998
Suffix: 8776655
Whole Number: 9988776655