サブストリングの代わりにチャンクでキー値ペアリングチャンクを照合しようとしていますので、一致するようにしています[email protected]
私は[email protected](?!|\S)
を使用しています私は部分文字列のマッチにヒットしないようにします。そして、https://regex101.com/r/ehuXFY/1によればそれは動作します。しかし、ここで私のユニットテストマッチは、文字列の末尾にある場合は、動作していないようです:あなたがre.search
を使用する必要があるときは、re.match
を使用しているターゲットが行末にある場合、Pythonの正規表現が一致しない
import unittest
import re
class MyRegexFuTestCases(unittest.TestCase):
def test_something(self):
lines = [
'[email protected]\talias= <[email protected]>\[email protected]\trip=8.8.8.8\tdate=1486528190\tsubject= Test Subject\[email protected]\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 = [
'[email protected]\talias= <[email protected]>\[email protected]\trip=8.8.8.8\tdate=1486528190\tsubject= Test Subject\[email protected]\treport=leoisafatcat\tattach_3=New List.xls']
whitelisted_pairs = ['[email protected]']
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 = [
'[email protected]\talias= <[email protected]>\[email protected]\trip=8.8.8.8\tdate=1486528190\tsubject= Test Subject\[email protected]\treport=leoisafatcat\tattach_3=New List.xls']
whitelisted_pairs = ['[email protected]']
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)
違いを説明できますか?なぜ 'from = test_email @ email.co'で' .com'でないのですか? –
@ jean-francoisFabreメールがemail.comであるため、email.coで一致させたくありません。私。私は部分文字列のマッチを望んでいません。 –
なぜこの複合体 '(?!\ S)'の代わりに単語境界に '\ b'を使用しないのですか? –