首先声明我对正则表达式很糟糕。我想在字符串中查找社会保障号的每个实例,并屏蔽除破折号 (-) 和 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”
慕仙森
相关分类