Reputation: 4432
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
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
Reputation: 5001
See my working example below.
^
to indicate the start of a line as well.[]
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.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
Reputation: 669
https://regex101.com/r/505NB9/1 It looks like the first two chars are not needed.
Upvotes: 0