fork download
  1. import re
  2. text = '+1 (123) 4567, 11111111, some text, 2222 2222, 333-33333-333, 444-444-444-4, some other text, 5555 5555 5555 5555 5, ab12345, xy7890z'
  3. rx = re.compile(r'''(?<!\w)(?: # Left-hand word boundary, start of a group:
  4. (?:\+\d{1,4}[\s-]*)? # Optional: `+` and 1 to 4 digits and then 0+ whitespace/-
  5. (?:\(\d+\)|\d{2,}) # (, 1+ digits, ), or 2 or more digits
  6. [-\s]* # 0+ whitespaces or hyphens
  7. \d+ # 1+ digits, and
  8. (?=([\s-]?))(?:\1\d+)* # 0 or more sequences of optional - or whitespace and 1+ digits
  9. | # or
  10. (?:[a-zA-Z]+\d|\d+[a-zA-Z]) # 1+ letters and then a digit, or 1+ digits and then a letter
  11. [a-zA-Z0-9]* # 0 or more letters or digits
  12. )\b # End of the group, word boundary''', re.VERBOSE)
  13. print( [x.group() for x in rx.finditer(text)] )
Success #stdin #stdout 0.03s 9360KB
stdin
Standard input is empty
stdout
['+1 (123) 4567', '11111111', '2222 2222', '333-33333-333', '444-444-444-4', '5555 5555 5555 5555 5', 'ab12345', 'xy7890z']