如果目标位于行的末尾,则Python正则表达式不匹配行

时间:2017-08-15 18:51:19

标签: python regex

我试图按块而不是子串匹配键值配对块,所以说我试图匹配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)

1 个答案:

答案 0 :(得分:2)

当您使用re.match时,您正在使用re.search

match尝试在行的开头匹配正则表达式。因此,只有最后两个测试用例匹配,因为它们从行的最开始开始。

另一方面,

search具有您期望的行为。它将正则表达式与行的任何部分进行匹配。