简体   繁体   English

使用正则表达式排除字符串?

[英]Exclude string using a regex?

I have some emails 我有一些电子邮件

info@gmail.com
epd@omi.ru
salesall@finteca.ru

I need to ignore strings that contain info, sales , so I used pattern: 我需要忽略包含info, sales字符串,因此我使用了pattern:

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

But it returns [] . 但它返回[] What am I doing wrong? 我究竟做错了什么?

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

See my working example below. 请参阅下面的工作示例。

  • For your code to work properly you will need to include ^ to indicate the start of a line as well. 为了使代码正常工作,您还需要包括^来指示行的开头。
  • The reason you got [] is probably because you didn't use the re.MULTILINE option. 得到[]的原因可能是因为您没有使用re.MULTILINE选项。 The re.MULTILINE flag tells python to make the '^' and '$' special characters match the start or end of any line within a string, as opposed to the start or end of the entire string. re.MULTILINE标志告诉python使'^'和'$'特殊字符匹配字符串中任何行的开头或结尾,而不是整个字符串的开头或结尾。

所需正则表达式的可视表示

import re

test = 'info@gmail.com\nepd@omi.ru\nsalesall@finteca.ru'
print(test)

info@gmail.com
epd@omi.ru
salesall@finteca.ru

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

['epd@omi.ru']

Perhaps more understandable and maintainable: 也许更容易理解和维护:

import re

string = """
info@gmail.com
epd@omi.ru
salesall@finteca.ru

some other text here with emails email@email.com included"""

rx = re.compile(r'\S+@\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)
# ['epd@omi.ru', 'email@email.com']

Simply adjust the lst of ignore() as needed. 只需调节lstignore()如需要的。

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

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