regex - Python Phone Number Mask -


i have 2 phone numbers , need mask them:

p:(123)456-1234

p:+1(123) 456-1234

to

p:***-***-1234

p:+*-***-***-1234

import re m = re.match(r'([0-9\s]+)', line) print(m.group(1)[-5:-1]) m = re.match(r'([0-9\s-+]+)', line) print(m.group(1)[-5:-1]) 

i'm getting result m none.

it not work because regex meant match phone number 11234561234

if pass strings containing 1 phone number through regex, it's easy, 2 parts interest : 1 won't masked, , part preceding it.

>>> import re >>> pat = "(.*)(-\d{4})$" >>> mask_part, public_part = re.match(pat,"+1(123) 456-1234").groups() >>> print re.sub("\d","*",mask_part) + public_part +*(***) ***-1234 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -