I'm trying to match a key-value pairing chunk by chunk instead of substrings, so say I'm trying to match from=email@email.com
I'm using from=email@email.com(?!|\S)
to make sure I don't hit on substring matches. And according to https://regex101.com/r/ehuXFY/1 it works. But here's my unit tests and the case where the match is at the end of the string doesn't seem to work:
import unittest
import re
class MyRegexFuTestCases(unittest.TestCase):
def test_something(self):
lines = [
'from=test_email@email.com\talias= <test_email@email.com>\trcptlist=test_recipient@email.com\trip=8.8.8.8\tdate=1486528190\tsubject= Test Subject\treply_to=test_email@email.com\treport=leoisafatcat\tattach_3=New List.xls']
whitelisted_pairs = ['attach_3=New List.xls']
lines = filter(lambda line: any(
map(lambda pair: not re.match(r'%s(?!\S)' % pair, line),
whitelisted_pairs)), lines)
self.assertEqual(len(lines), 0)
def test_another_case(self):
lines = [
'from=test_email@email.com\talias= <test_email@email.com>\trcptlist=test_recipient@email.com\trip=8.8.8.8\tdate=1486528190\tsubject= Test Subject\treply_to=test_email@email.com\treport=leoisafatcat\tattach_3=New List.xls']
whitelisted_pairs = ['from=test_email@email.com']
lines = filter(lambda line: any(
map(lambda pair: not re.match(r'%s(?!\S)' % pair, line),
whitelisted_pairs)), lines)
self.assertEqual(len(lines), 0)
def test_no_match(self):
lines = [
'from=test_email@email.com\talias= <test_email@email.com>\trcptlist=test_recipient@email.com\trip=8.8.8.8\tdate=1486528190\tsubject= Test Subject\treply_to=test_email@email.com\treport=leoisafatcat\tattach_3=New List.xls']
whitelisted_pairs = ['from=test_email@email.co']
lines = filter(lambda line: any(
map(lambda pair: not re.match(r'%s(?!\S)' % pair, line),
whitelisted_pairs)), lines)
self.assertEqual(len(lines), 1)
if __name__ == '__main__':
unittest.main()
..F
======================================================================
FAIL: test_something (__main__.MyRegexFuTestCases)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/stupidfatcat/PycharmProjects/adhoc/so_help.py", line 13, in test_something
self.assertEqual(len(lines), 0)
AssertionError: 1 != 0
----------------------------------------------------------------------
Ran 3 tests in 0.001s
FAILED (failures=1)