Petr Petrov
Petr Petrov

Reputation: 4432

Exclude string using a regex?

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?

Upvotes: 2

Views: 3323

Answers (3)

Jan
Jan

Reputation: 43169

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.

Upvotes: 1

bluprince13
bluprince13

Reputation: 5001

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]']

Upvotes: 0

Kroustou
Kroustou

Reputation: 669

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

Upvotes: 0

Related Questions