简体   繁体   English

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

[英]Non greedy regex pattern for email Python

I m new to regular expression and stuck at this situation where my current regex pattern is returning two results and one of them starting with the phone no.我是正则表达式的新手,并且陷入了这种情况,我当前的正则表达式模式返回两个结果,其中一个以电话号码开头。 (String doesn't have a space between phone no and email). (字符串在电话号码和电子邮件之间没有空格)。 I tried using the?我尝试使用? to make expression non greedy but its not returning anything.使表达式不贪婪,但不返回任何内容。

Code代码
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 Output
9012345678testemail.grp@gmail.com
support@testtest.com

I guess what you are looking for is:我想你正在寻找的是:

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

[a-zA-Z] -> the email starts with a letter [a-zA-Z] -> email 以字母开头

\w -> any letter, digit or an underscores \w -> 任何字母、数字或下划线

[\w.-]* -> 0 or more letters, digits, underscores, ., or - [\w.-]* -> 0 个或多个字母、数字、下划线、. 或 -

[az]+ -> a lower case domain 1 or more characters long [az]+ -> 1 个或多个字符长的小写域

[.] -> put the . [.] -> 把. in square brackets, cuz.在方括号中,因为。 has a special meaning in regex在正则表达式中有特殊含义

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)

Result:结果:

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

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

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