我有一些电子邮件使用正则表达式排除字符串?
[email protected]
[email protected]
[email protected]
我需要忽略包含info, sales
字符串,所以我用的模式:
'/(?!spb)[a-zA-Z0-9-_\.][email protected][a-z0-9\.]+$'
但它返回[]
。我究竟做错了什么?
我有一些电子邮件使用正则表达式排除字符串?
[email protected]
[email protected]
[email protected]
我需要忽略包含info, sales
字符串,所以我用的模式:
'/(?!spb)[a-zA-Z0-9-_\.][email protected][a-z0-9\.]+$'
但它返回[]
。我究竟做错了什么?
https://regex101.com/r/505NB9/1它看起来像前两个字符是不需要的。
我觉得没有。因为它灯串,不符合模式 –
是的抱歉,我不明白这个问题。尽管你可以避免使用正则表达式: 'email.split'('@')[0]或email.split('@')'sales'中的'if'info':' – Kroustou
看到我下面的工作示例。
^
以指示行的开始。[]
的原因可能是您没有使用re.MULTILINE选项。 re.MULTILINE标志告诉python使'^'和'$'特殊字符匹配字符串中任何行的开始或结束,而不是整个字符串的开始或结束。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]']
也许更易懂,易维护:根据需要
import re
string = """
[email protected]
[email protected]
[email protected]
some other text here with emails [email protected] included"""
rx = re.compile(r'\[email protected]\S+')
def ignore(value):
lst = ['info', 'sales']
for i in lst:
if i in value:
return False
return True
emails = filter(ignore, rx.findall(string))
print(emails)
# ['[email protected]', '[email protected]']
简单地调整ignore()
的lst
。
向我们展示您尝试使用的代码。你想排除他们什么?一个列表,一个字典,一组? –
@AustinHastings我在我的问题中指定了这些数据。我试图用电子邮件在样本中测试。为此,我使用'emails = re.findall(pattern,test)' –
https://regex101.com/r/505NB9/3 – JazZ