-1
import re
s = ' 1.210.223.1112 (113)-945-6373; 222.212.2333 (111)123-4567 (111)1234567 (111 )123-4567 \n(1-100-533-1710)the  ( 111)123-4567 Z1902022 (111)123-4567 N18-2181 Z1234567 1-22-2001 (100) 115-5198 f: (110)-111-1111 112.222.1112 (222)-222-3337 (120) 305-1314' 

I have a list of phone numbers and I would like to get all variations present in the string s. From modification of selecting variations of phone numbers using regex I have tried the following

reg = r'\(?\s*\d?\s*-?\d{3}\s*[)-]?-?\s*\d{3}\s*-?\s*\d{4}\)?'
r1 = re.findall(reg,s)
r1

Which gives me close to what I want

['(113)-945-6373',
 '(111)123-4567',
 '(111)1234567',
 '(111 )123-4567',
 '(1-100-533-1710)',
 '( 111)123-4567',
 '(111)123-4567',
 '(100) 115-5198',
 '(110)-111-1111',
 '(222)-222-3337',
 '(120) 305-1314']

However, it is missing 1.210.223.1112 and 222.212.2333.

I have tried modifying reg by adding a . but this doesn't give me what I am looking for

reg = r'\(?\s.*\d?\s*-?\d{3}\s*[)-]?-?\s*\d{3}\s*-?\s*\d{4}\)?'

My desired output is as follow which includes 1.210.223.1112 and 222.212.2333

['1.210.223.1112',
 '(113)-945-6373',
  '222.212.2333',
 '(111)123-4567',
 '(111)1234567',
 '(111 )123-4567',
 '(1-100-533-1710)',
 '( 111)123-4567',
 '(111)123-4567',
 '(100) 115-5198',
 '(110)-111-1111',
 '(222)-222-3337',
 '(120) 305-1314']

How do I alter reg to get my desired output?

Toto
  • 89,455
  • 62
  • 89
  • 125
ChiBay
  • 189
  • 1
  • 6

2 Answers2

4

You were close, just add an optional dot match \.? when you're looking for - , as well as separate the case when you have ( whitespace and a whitespace in front of number:

(?:(?:\(\s)|\(?\d?)-?\.?\d{3}\s*[)-]?-?\.?\s*\d{3}\s*-?\.?\s*\d{4}\)?

Regex Demo

Equivalent, but using OR | and non-matching groups :

(?:(?:\(\s)|\(?\d?)(?:-|\.)?\d{3}\s*[)-]?(?:-|\.)?\s*\d{3}\s*(?:-|\.)?\s*\d{4}\)?

Regex Demo

Output:

['1.210.223.1112',
 '(113)-945-6373',
 '222.212.2333',
 '(111)123-4567',
 '(111)1234567',
 '(111 )123-4567',
 '1-100-533-1710',
 '( 111)123-4567',
 '(111)123-4567',
 '(100) 115-5198',
 '(110)-111-1111',
 '112.222.1112',
 '(222)-222-3337',
 '(120) 305-1314']
vs97
  • 5,765
  • 3
  • 28
  • 41
0
re.findall(r'(?:\b\d[.-])?\(?\s?\d{3}[.) -]{0,2}\d{3}[ .-]?\d{4}',s)
Out[31]: 
['1.210.223.1112',
 '(113)-945-6373',
 ' 222.212.2333',
 '(111)123-4567',
 '(111)1234567',
 '(111 )123-4567',
 '1-100-533-1710',
 '( 111)123-4567',
 '(111)123-4567',
 '(100) 115-5198',
 '(110)-111-1111',
 ' 112.222.1112',
 '(222)-222-3337',
 '(120) 305-1314']
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • 2
    @EER Be aware that this answer matches whitespace in front of the number with dots, e.g. ' 222.212.2333' and ' 112.222.1112'. – vs97 Oct 02 '19 at 23:37