re.match
与正确的正则表达式一起使用。
import re
r = re.compile(r'^[a-fA-F0-9]{32}$')
with open("hashes") as i, open("hashes_cleaned", "w") as o:
for line in i:
# Cheap check for 32 chars, first.
if len(line.strip()) == 32 and r.match(line.strip()):
o.write(line)
例子
输入:
% cat hashes
c2cf0d7b2d3c5cd91a1314a2285ce53e
12524f7af3a5dad467264683d0ec6206
746518a1c63294d367c23cab37f4166c
foo
4d6deee14efe78180c698679e16f7342
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
eb9a25b6b525ae665115b43a259d2355
输出:
% cat hashes_cleaned
c2cf0d7b2d3c5cd91a1314a2285ce53e
12524f7af3a5dad467264683d0ec6206
746518a1c63294d367c23cab37f4166c
4d6deee14efe78180c698679e16f7342
eb9a25b6b525ae665115b43a259d2355