fork(1) download
  1. import re
  2.  
  3. data = """This is my phone number and I am 25 years old, 999-888-7894 and I am looking for a regex script.
  4. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  5. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 999-123-4567 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  6. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  7. And 555-555-1212 is my telephone."""
  8.  
  9. phno = re.compile(r'\b(?:phone|ph|telephone)\b.{0,49}\b(\d{3}[-]\d{3}[-]\d{4})\b|\b(\d{3}[-]\d{3}[-]\d{4})\b.{0,49}\b(?:phone|ph|telephone)\b', flags=re.I)
  10.  
  11. phones = [m.group(1) if m.group(1) else m.group(2) for m in phno.finditer(data)]
  12. print(phones)
  13.  
Success #stdin #stdout 0.02s 9812KB
stdin
Standard input is empty
stdout
['999-888-7894', '555-555-1212']