2017-02-04 44 views
2

我有一些电子邮件使用正则表达式排除字符串?

[email protected] 
[email protected] 
[email protected] 

我需要忽略包含info, sales字符串,所以我用的模式:

'/(?!spb)[a-zA-Z0-9-_\.][email protected][a-z0-9\.]+$' 

但它返回[]。我究竟做错了什么?

+3

向我们展示您尝试使用的代码。你想排除他们什么?一个列表,一个字典,一组? –

+0

@AustinHastings我在我的问题中指定了这些数据。我试图用电子邮件在样本中测试。为此,我使用'emails = re.findall(pattern,test)' –

+0

https://regex101.com/r/505NB9/3 – JazZ

回答

0

https://regex101.com/r/505NB9/1它看起来像前两个字符是不需要的。

+0

我觉得没有。因为它灯串,不符合模式 –

+0

是的抱歉,我不明白这个问题。尽管你可以避免使用正则表达式: 'email.split'('@')[0]或email.split('@')'sales'中的'if'info':' – Kroustou

0

看到我下面的工作示例。

  • 为使您的代码正常工作,您还需要包含^以指示行的开始。
  • 您得到[]的原因可能是您没有使用re.MULTILINE选项。 re.MULTILINE标志告诉python使'^'和'$'特殊字符匹配字符串中任何行的开始或结束,而不是整个字符串的开始或结束。

Visual representation of the required regular expression

import re 

test = '[email protected]\[email protected]\[email protected]' 
print(test) 

[email protected] 
[email protected] 
[email protected] 

pattern = re.compile('^(?!info|sales)[[a-zA-Z0-9-_.][email protected][a-z0-9.]+$', re.MULTILINE) 
emails = re.findall(pattern, test) 
print(emails) 

['[email protected]']