为了账号安全,请及时绑定邮箱和手机立即绑定

仅使用具有多个 SSN 的文件中的部分掩码来屏蔽所有 SSN

仅使用具有多个 SSN 的文件中的部分掩码来屏蔽所有 SSN

Helenr 2023-06-21 14:55:45
首先声明我对正则表达式很糟糕。我想在字符串中查找社会保障号的每个实例,并屏蔽除破折号 (-) 和 SSN 的最后 4 个之外的所有实例。例子String someStrWithSSN = "This is an SSN,123-31-4321, and here is another 987-65-8765";Pattern formattedPattern = Pattern.compile("^\\d{9}|^\\d{3}-\\d{2}-\\d{4}$");Matcher formattedMatcher = formattedPattern.matcher(someStrWithSSN);while (formattedMatcher.find()) {    // Here is my first issue.  not finding the pattern}// my next issue is that I need to my String should look like this//     "This is an SSN,XXX-XX-4321, and here is another XXX-XX-8765"预期结果是找到每个 SSN 并替换。上面的代码应生成字符串“这是一个 SSN,XXX-XX-4321,这里是另一个 XXX-XX-8765”
查看完整描述

1 回答

?
慕仙森

TA贡献1827条经验 获得超7个赞

您可以通过执行以下操作来简化此操作:


String initial = "This is an SSN,123-31-4321, and here is another 987-65-8765";

String processed = initial.replaceAll("\\d{3}\\-\\d{2}(?=\\-\\d{4})","XXX-XX");

System.out.println(initial);

System.out.println(processed);

输出:


这是一个 SSN,123-31-4321,这里是另一个 987-65-8765

这是一个 SSN,XXX-XX-4321,这里是另一个 XXX-XX-8765


正则表达式\d{3}\-\d{2}(?=\-\d{4})捕获三个数字后跟两个数字,用破折号分隔(然后是破折号和 4 位数字,非捕获)。与此正则表达式一起使用replaceAll将创建所需的掩蔽效果。


编辑:


如果您还希望此替换针对 9 个连续数字,您可以执行以下操作:


String initial = "This is an SSN,123-31-4321, and here is another 987658765";

String processed = initial.replaceAll("\\d{3}\\-\\d{2}(?=\\-\\d{4})","XXX-XX")

                       .replaceAll("\\d{5}(?=\\d{4})","XXXXX");

System.out.println(initial);

System.out.println(processed);

输出:


这是一个 SSN,123-31-4321,这里是另一个 987658765

这是一个 SSN,XXX-XX-4321,这里是另一个 XXXXX8765


正则表达式\d{5}(?=\d{4})捕获 5 位数字(后跟 4 位数字,非捕获)。使用第二次调用replaceAll将使用适当的替换来定位这些序列。


编辑: 这是以前的正则表达式的更强大的版本,以及新正则表达式如何工作的更长演示:


String initial = "123-45-6789 is a SSN that starts at the beginning of the string,

    and still matches. This is an SSN, 123-31-4321, and here is another 987658765. These

    have 10+ digits, so they don't match: 123-31-43214, and 98765876545.

    This (123-31-4321-blah) has 9 digits, but is followed by a dash, so it doesn't match.

    -123-31-4321 is preceded by a dash, so it doesn't match as well. :123-31-4321 is 

    preceded by a non-colon/digit, so it does match. Here's a 4-2-4 non-SSN that would've

    tricked the initial regex: 1234-56-7890. Here's two SSNs in parentheses: (777777777) 

    (777-77-7777), and here's four invalid SSNs in parentheses: (7777777778) (777-77-77778)

    (777-778-7777) (7778-77-7777). At the end of the string is a matching SSN:

    998-76-4321";

String processed = initial.replaceAll("(?<=^|[^-\\d])\\d{3}\\-\\d{2}(?=\\-\\d{4}([^-\\d]|$))","XXX-XX")

                       .replaceAll("(?<=^|[^-\\d])\\d{5}(?=\\d{4}($|\\D))","XXXXX");

System.out.println(initial);

System.out.println(processed);

输出:

123-45-6789 是从字符串开头开始的 SSN,并且仍然匹配。这是一个 SSN,123-31-4321,这是另一个 987658765。这些有 10 多个数字,因此它们不匹配:123-31-43214 和 98765876545。这个(123-31-4321-blah)有9 位数字,但后面有破折号,因此不匹配。-123-31-4321 前面有破折号,因此也不匹配。:123-31-4321 前面有一个非冒号/数字,因此它确实匹配。这是一个 4-2-4 非 SSN,它会欺骗初始正则表达式:1234-56-7890。括号中有两个 SSN:(777777777) (777-77-7777),括号中有四个无效 SSN:(7777777778)(777-77-77778) (777-778-7777) (7778-77-7777)。字符串末尾是匹配的 SSN:998-76-4321

XXX-XX-6789 是从字符串开头开始的 SSN,并且仍然匹配。这是 SSN,XXX-XX-4321,这是另一个 XXXXX8765。它们有 10 个以上的数字,所以它们不匹配:123-31-43214 和 98765876545。这个 (123-31-4321-blah) 有 9 个数字,但后面跟着一个破折号,所以它不匹配。-123-31-4321 前面有一个破折号,所以它也不匹配。:XXX-XX-4321 前面有一个非冒号/数字,所以它匹配。这是一个 4-2-4 非 SSN,它会欺骗初始正则表达式:1234-56-7890。括号中有两个 SSN:(XXXXX7777) (XXX-XX-7777),括号中有四个无效 SSN:(7777777778)(777-77-77778) (777-778-7777) (7778-77-7777)。字符串末尾是匹配的 SSN:XXX-XX-4321


查看完整回答
反对 回复 2023-06-21
  • 1 回答
  • 0 关注
  • 119 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信