在我的程序中,我将接受两种不同格式的16位数字,并屏蔽其间的数字。 (它只会显示前6个数字,后4个数字)
当前,代码中的正则表达式仅掩盖数字,不带空格。有人可以帮我编辑正则表达式,以便它也掩盖带有空格的数字吗?
import csv
import re
find_and_mask_cc = lambda s: re.sub(r'(\d{6})\d{6}(\d{4})', r'\1XXXXXX\2', s)
with open('CSV_sample_file.csv') as file1, open('CSV_sample_file2.csv', 'w') as file2:
reader = csv.reader(file1)
writer = csv.writer(file2)
writer.writerow(next(reader))
writer.writerows(map(find_and_mask_cc, row) for row in reader)
答案 0 :(得分:0)
answer = str(input).replace(' ','')
答案 1 :(得分:0)
要么用空字符串替换空格,要么在正则表达式中包含可选的空格:r'(\d{6}\s?)\d{6}\s?(\d{4})'
答案 2 :(得分:0)
reg = "((?<=^\d{6})|(?<=^\d{4} \d{2}))(.*)(?=\d{4}$)"
str1 = "1111111111111111"
str2 = "1111 1111 1111 1111"
m1 = re.search(reg, str1)
m2 = re.search(reg, str2)
res1 = str1[:m1.start()] + re.sub("\d", "*", m1.group(0)) + str1[m1.end():]
res2 = str2[:m2.start()] + re.sub("\d", "*", m2.group(0)) + str2[m2.end():]
>>> res1
'111111******1111'
>>> res2
'1111 11** **** 1111'