简体   繁体   English

部分替换为 re.sub()

[英]Partial replacement with re.sub()

Let's say I want to find all credit card numbers in a 'text' and replace the first three 4-digit groups with XXXX, leaving the last group as it is.假设我想在“文本”中找到所有信用卡号,并用 XXXX 替换前三个 4 位数字组,保留最后一组。

How can I do this with re.sub()?我怎样才能用 re.sub() 做到这一点?

My best try so far is到目前为止我最好的尝试是

re.sub(r"(\d{4}-){3}", "XXXX-XXXX-XXXX-", text)

But of course this pattern would cause a replacement in non-credit card expressions like '1234-5678-1234-asdfg'.但当然,这种模式会导致替换非信用卡表达式,如“1234-5678-1234-asdfg”。

You could use a lookahead assertion:您可以使用前瞻断言:

re.sub(r"(\d{4}-){3}(?=\d{4})", "XXXX-XXXX-XXXX-", text)

Eg:例如:

In [1]: import re

In [2]: text = '1234-5678-9101-1213 1415-1617-1819-hello'

In [3]: re.sub(r"(\d{4}-){3}(?=\d{4})", "XXXX-XXXX-XXXX-", text)
Out[3]: 'XXXX-XXXX-XXXX-1213 1415-1617-1819-hello'

Though this would match asdf1234-4567-1234-4567-asdf as well.虽然这也会匹配 asdf1234-4567-1234-4567-asdf。

Another way using a backreference:使用反向引用的另一种方法:

data = "4220-1234-9948-2245 is a cc num i have and so is 4153-4222-3942-4852 but dont tell anyone"
print re.sub(r"(\d{4}-){3}(\d{4})", "XXXX-XXXX-XXXX-\\2", data)

# XXXX-XXXX-XXXX-2245 is a cc num i have and so is XXXX-XXXX-XXXX-4852 but dont tell anyone

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM