[英]Python - Regex - findall duplicates
I'm trying to match e-mails in html text using the following code in python 我正在尝试使用python中的以下代码匹配html文本中的电子邮件
my_second_pat = '((\w+)( *?))(@|[aA][tT]|\([aA][tT]\))(((( *?)(\w+)( *?))(\.|[dD][oO][tT]|\([dD][oO][tT]\)))+)([eE][dD][uU]|[cC][oO][mM])'
matches = re.findall(my_second_pat,line)
for m in matches:
s = "".join(m)
email = "".join(s.split())
res.append((name,'e',email))
when I run it on a line = shoham@stanford.edu
当我在line = shoham@stanford.edu
上运行它时line = shoham@stanford.edu
I get: 我得到:
[('shoham', 'shoham', '', '@', 'stanford.', 'stanford.', 'stanford', '', 'stanford', '', '.', 'edu')]
what I expect: 我所期望的:
[('shoham','@', 'stanford.', 'edu')]
It's matched as a one string on regexpal.com, so I guess I'm having trouble with re.findall
它在regexpal.com上作为一个字符串匹配,所以我想我在re.findall
上遇到麻烦
I'm new to both regex, and python. 我是regex和python的新手。 Any optimization/modifications is welcomed. 欢迎进行任何优化/修改。
It is matching all of your capture groups, which contain optional matches. 它与您的所有捕获组都匹配,其中包含可选匹配项。
((?:(?:\w+)(?: *?))(?:@|[aA][tT]|\(?:[aA][tT]\))(?:(?:(?:(?: *?)(?:\w+)(?: *?))(?:\.|[dD][oO][tT]|\(?:[dD][oO][tT]\)))+)(?:[eE][dD][uU]|[cC][oO][mM]))
Try this: 尝试这个:
(?i)([^@\s]{2,})(?:@|\s*at\s*)([^@\s.]{2,})(?:\.|\s*dot\s*)([^@\s.]{2,})
If you need to limit to .com
and .edu
: 如果您需要限制.com
和.edu
:
(?i)([^@\s]{2,})(?:@|\s*at\s*)([^@\s.]{2,})(?:\.|\s*dot\s*)(com|edu)
Note that I have used the case-insensitive flag (?i)
at the start of the regex, instead of using syntax like [Ee]
. 注意,我在正则表达式的开头使用了不区分大小写的标志(?i)
,而不是使用[Ee]
这样的语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.