我目前使用以下代码:
regexp = re.compile(r'[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}', re.IGNORECASE)
email = regexp.findall(stext)
if email:
email = email[0]
但即使stext包含除电子邮件之外的其他文本(例如,some text email@example.com some other text),它也可以工作。当我想检查是否仅stext包含(好的,可能带有前导或/和尾随空格,没有其他字符)时。email@example.com
PS。我还认为可能我应该使用mail.is_email_valid而不是正则表达式,它将允许我支持长域,.museam如.рф.