Python 3中列表交集中的模式匹配

时间:2018-04-27 10:03:01

标签: python regex intersection

我是Python的初学者,我的目标是编写一个驻留在git服务器上的预接收挂钩。

如果任何消息未通过格式检查,钩子应该检查提交消息的格式并拒绝推送。

我认为我对此进行了简单的提交模式比较,如下所示:

commit_list = ['test1', 'test2', 'test3', 'test4']
patterns = ['test1','test2']

matched = set(commit_list).intersection(patterns) # find matches

for match in matched:
    commit_list[:] = (commit for commit in commit_list if commit != match) # remove matches from commit_list

commit_list_length = len(commit_list)

if commit_list_length == 0: # all commits matched so the list is empty
    print('all commit messages match the format')
else:
    print('%s of the commit messages don\'t match the format' % commit_list_length)
    print(commit_list)

如何修改此代码段以包含regexp,例如:re.match(pattern,commit)

因此,当第二行更改为例如:

时,它仍然有效
patterns = ['test[0-9]{1,5}','test: ']

2 个答案:

答案 0 :(得分:0)

如果您想更改代码以包含re.match(),可以尝试此操作。

import re
commit_list = ['test1', 'test2', 'test3', 'test4']
patterns = ['test1','test2']
regx=re.compile(r'{}'.format("|".join(patterns)))
commit_unmatched=[commit for commit in commit_list if not regx.match(commit)]
print(commit_unmatched)

答案 1 :(得分:-1)

我认为我找到了一个有效的解决方案,但几乎可以肯定,它不是最短或最有效的解决方案:

commit_list = ['test1', 'test234', 'testA', 'test:a', 'test: a']
patterns = ['test[0-9]{1,5}','test: ']

cloned_list = list(commit_list) # copy list of commits

for item in commit_list:
    for pattern in patterns:
        if re.match(pattern,item):
            cloned_list.remove(item) # work on the copy and discard matched
        elif not re.match(pattern,item):
            continue # exhaust all possibilities

cloned_list_length = len(cloned_list)

if cloned_list_length == 0: # all commits matched so the list is empty
    print('all commit messages match the format')
else:
    print('%s of the commit messages don\'t match the criteria' % cloned_list_length)
    print(cloned_list) # only print unmatched

仍然愿意尝试更好的建议!