2

I have some emails

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

I need to ignore strings that contain info, sales, so I used pattern:

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

But it returns []. What am I doing wrong?

4
  • 3
    Show us the code you're trying to use. What do you want to exclude them from? A list, a dict, a set? Commented Feb 4, 2017 at 18:10
  • @AustinHastings I specify this data in my question. I tried to test that in sample with email. And to do that I use emails = re.findall(pattern, test) Commented Feb 4, 2017 at 18:19
  • regex101.com/r/505NB9/3 Commented Feb 4, 2017 at 19:07
  • Try re.findall(r'(?m)^(?!.*(?:info|sales))\S+@\S+\.\S+$', s) Commented Feb 4, 2017 at 20:08

3 Answers 3

1

Perhaps more understandable and maintainable:

import re

string = """
[email protected]
[email protected]
[email protected]

some other text here with emails [email protected] 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)
# ['[email protected]', '[email protected]']

Simply adjust the lst of ignore() as needed.

Sign up to request clarification or add additional context in comments.

Comments

0

https://regex101.com/r/505NB9/1 It looks like the first two chars are not needed.

2 Comments

I think no. because it light string, that not match pattern
Yes sorry i did not understand the the question. You could avoid regex though: if 'info' in email.split('@')[0] or 'sales' in email.split('@')[0]:
0

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. 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.

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-_.]+@[a-z0-9.]+$', re.MULTILINE)
emails = re.findall(pattern, test)
print(emails)

['[email protected]']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.