1.使用正则完成下列内容的匹配
- 匹配陕西省区号 029-12345
- 匹配邮政编码 745100
- 匹配邮箱 lijian@xianoupeng.com
- 匹配身份证号 62282519960504337X

import re
# 定义各类内容的正则表达式
patterns = {
    "陕西省区号": r"^029-\d{5,}$",
    "邮政编码": r"^\d{6}$",
    "邮箱": r"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$",
    "身份证号": r"^\d{17}[\dXx]$"
}
test_cases = {
    "陕西省区号": ["029-12345", "029-678901"],
    "邮政编码": ["745100", "100000"],
    "邮箱": ["lijian@xianoupeng.com", ],
    "身份证号": ["62282519960504337X"]
}

def match_content():
    for content_type, pattern in patterns.items():
        print(f"\n===== 匹配【{content_type}】=====")
        regex = re.compile(pattern)
        for case in test_cases[content_type]:
            result = "匹配成功" if regex.match(case) else "匹配失败"
            print(f"{case} → {result}")

match_content()

更多推荐