Files
task-2-4-regular-expression/4.2作业.py
2509165003 1e73b5f504 4.2
2026-04-02 16:01:37 +08:00

15 lines
279 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
phone_book = '''
张三138-1999-9101
李四139-1998-5678
王五138-1997-1234
'''
pattern = r'(\d{3})-(\d{4})-(\d{4})'
def mask_phone(match):
return f'{match.group(1)}-****-{match.group(3)}'
masked = re.sub(pattern, mask_phone, phone_book)
print(masked)