繁体   English   中英

email Python 的非贪婪正则表达式模式

[英]Non greedy regex pattern for email Python

我是正则表达式的新手,并且陷入了这种情况,我当前的正则表达式模式返回两个结果,其中一个以电话号码开头。 (字符串在电话号码和电子邮件之间没有空格)。 我尝试使用? 使表达式不贪婪,但不返回任何内容。

代码
pattern = re.compile(r'\b[A-Za-z0-9._%.?-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b')

matches = pattern.finditer(Clean)

for match in matches:
   print(match.group(0))
Output
9012345678testemail.grp@gmail.com
support@testtest.com

我想你正在寻找的是:

pattern = re.compile(r"[a-zA-Z]+[\w.-]*@[a-z]+[.][a-z]+")

[a-zA-Z] -> email 以字母开头

\w -> 任何字母、数字或下划线

[\w.-]* -> 0 个或多个字母、数字、下划线、. 或 -

[az]+ -> 1 个或多个字符长的小写域

[.] -> 把. 在方括号中,因为。 在正则表达式中有特殊含义

pattern = re.compile(r"[a-zA-Z]+[\w.-]*@[a-z]+[.][a-z]+")
matches = pattern.findall("aar@g.com ooo11..com hellow_world@hello.world 1234hey@hey.com")
print(matches)

结果:

['aar@g.com', 'hellow_world@hello.world', 'hey@hey.com']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM