在Python中,我正在尝试匿名化或掩盖n个字符(从最后一个字符开始)。它可以用于匹配,但我想忽略同时包含数字的单词。
例如:
例如:
string = 'SomeText +12 555 660 000 f00b4r abc123'
digits = 5
repl_char = 'X'
regex = '[0-9\s\+]+'
for match in re.finditer(regex, string):
phone_no = match.group()
new_ph_no = ''
i = 0
if phone_no in ['', ' ']:
pass
else:
for phone_digit in phone_no[::-1]:
if phone_digit == ' ' or i >= digits:
new_ph_no += phone_digit
else:
new_ph_no += repl_char
i += 1
string = string.replace(phone_no, new_ph_no[::-1])
print(string)
目前输出 : 'SomeText +x2 555 6xx xxx fxxbxr abcxxx'
期望输出 : 'SomeText +12 555 6XX XXX f00b4r abc123'
我尝试在正则表达式中添加'[^?!a-zA-Z(0-9).*$]'
以获得期望的输出,但是当我将字符串传递为'SomeText +12 555 660 000'
时会失败。
我的意图是从任何类型的字符串中打印电话号码,并用“x”(或任何字符)替换n个数字。
如何打印所需的输出?
660
的第二个数字上标记,而不是在555
的第二个数字上标记? - anubhavadigits = 5
,我认为数字部分的最后5位需要被替换,是吗? - Austin