我正在从给定的字符串中查找一封电子邮件并返回该字符串中存在的电子邮件

I am finding an email from from a given string and returning the email that exists in the string

我的要求是-

name:名称是小于或等于的字母数字字符串 等于 12 个字符。允许的其他字符是 破折号 (-)、句点 (.) 和下划线 (_)。但是电子邮件 不能以这些附加字符开始或结束。 该名称的长度也必须至少为 1 个字符。 示例名称值: 一个, ab, a_b, A__B..C--D, 1nt3r3st.1ng

domain: domain 是严格的数字,数字必须是 可被 5 整除。域的长度不受限制。 域值示例: 984125, 0

结尾:电子邮件必须以 (.com) 或 (.ca) 结尾(区分大小写)

示例:

find_special_email('12345a_test_email@165265365.com!')

'a_test_email@165265365.com'

我试过的:

import re

def find_special_email(str):
   match = re.search(r'[a-zA-Z0-9_\.-]{1,12}@[0-9]+\.(com|ca)(\.[a-z]{2,3})?', str)
   return match.group(0)


print(find_special_email('12345a_test_email@165265365.com!'))
print(find_special_email('A__B..C--D@165265365.com!'))

我的问题:

  1. the email cannot start or end with these additional characters e.g dash (-), period (.) and underscore (_)
  2. I don't know how to match the "domain" that is divisible by 5

这个正则表达式 - https://regex101.com/r/wSS0ES/4 可以提供帮助。

正则表达式: [a-zA-Z0-9](?:[a-zA-Z0-9_.-]{0,10}[a-zA-Z0-9])?@[0-9]*[05]+\.(?:com|ca)(?:\.[a-z]{2,3})?

所做的更改:

  1. 前缀 [a-zA-Z0-9] 以便电子邮件以有效字符开头。
  2. (?:[a-zA-Z0-9_.-]{0,10}[a-zA-Z0-9])? - 在第一个字符之后,地址可以选择包含中间所有有效字符的 0 到 10 倍,但应该以字母数字字符结尾。整个表达式是可选的,因此它可以匹配地址部分中的单个有效字符。
  3. [0-9]*[05]+ - 它确保域可以包含多个数字,但它应该以 0 或 5
  4. 结尾