我试图按块而不是子串匹配键值配对块,所以说我试图匹配from=email@email.com
我使用from=email@email.com(?!|\S)
来匹配确保我没有点击子串匹配。根据{{3}}它的确有效。但是这里我的单元测试以及匹配在字符串末尾的情况似乎不起作用:
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)