Files
task-2-4-regular-expression/text4.py
2026-04-02 16:07:32 +08:00

18 lines
373 B
Python
Raw 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-1234-5678
李四139-5678-1234
王五138-0000-1111
'''
# 脱敏:将 138-****-5678 格式输出
# 提示:使用分组和 re.sub
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)