python正则表达式匹配邮箱


# -*- coding:utf-8 -*-
import re
pattern = r'^[0-9a-zA-Z_]{0,19}@[0-9a-zA-Z]{1,13}\.[com,cn,net]{1,3}$'
thing = 'if you\'re interested in learning more about our app and how you can get involved,' \
        'please don\'t hesitate to contact me at john.doe@example.com or our team at info@hunnu.edu.cn and someoneaddress@csu.edu.cn. ' \
        'We\'d be happy to answer any questions you may have and provide you with more details about our launch plans.'\
match = re.findall(pattern,thing,re.I)
print(match)

一直提示match那里有语法错误,但是语法错误在哪呢好绝望

img

两个问题,第一个,你的 thing 赋值,多行之间用 \ 分隔没问题,最后一行你也加这个?是想和 match = 放到一起吗?

第二个,[com,cn,net]{1,3} 是什么鬼? 你是想 .cte 也可以匹配么?[] 是字符集定义啊亲

尝试用re.match来匹配

import re

# 定义待匹配的字符串,即邮箱地址
email = "测试案例"

# 定义正则表达式模式
pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"

# 匹配字符串
match = re.match(pattern, email)

# 判断是否匹配成功
if match:
    print("匹配成功!")
else:
    print("匹配失败。")


findall返回的是一个列表,需要用循环遍历