To mask any 8-digit continuous number in a string, including
cases where there might be special characters adjacent to the digits, you can
use regular expressions (regex) to identify and replace these patterns.
Requirements:
- Identify
sequences of exactly 8 digits, even if they are adjacent to special
characters.
- Replace
these sequences with a masking string (e.g., ********).
Approach:
- Use a
regex pattern to match any 8-digit continuous numbers.
- Replace
each match with the masking string.
Example Code
Here's a Java example that demonstrates how to achieve this:
Java
import
java.util.regex.Matcher; import
java.util.regex.Pattern; public class
MaskEightDigitNumbersWithSpecialChars { // Function to mask all 8-digit numbers
in a given string public static String
maskEightDigitNumbers(String input) { // Define the regex pattern to find
8-digit numbers String regex =
"(?<!\\d)\\d{8}(?!\\d)"; // Compile the pattern Pattern pattern =
Pattern.compile(regex); // Create a matcher from the pattern Matcher matcher =
pattern.matcher(input); // Define the mask string String mask = "********"; // Use StringBuffer to collect the
result StringBuffer result = new
StringBuffer(); // Replace all 8-digit numbers with
the mask while (matcher.find()) { matcher.appendReplacement(result,
mask); } matcher.appendTail(result); return result.toString(); } public static void main(String[] args) { // Example strings with 8-digit
numbers and special characters String[] testInputs = { "Here are some numbers:
12345678 and 87654321. Also, 1234 and 123456789 are here.", "Special characters:
*12345678* and #87654321#.", "Mixed in text:
text12345678text and other87654321other.", "Digits with special
characters: 12-345678 or 8765-4321." }; // Process each string and print the
result for (String input : testInputs) { String output =
maskEightDigitNumbers(input);
System.out.println("Original: " + input);
System.out.println("Masked:
" + output); System.out.println(); } } } |
Explanation
- Regex
Pattern: (?<!\\d)\\d{8}(?!\\d)
- (?<!\\d):
Negative lookbehind assertion to ensure the 8 digits are not preceded by
another digit.
- \\d{8}:
Matches exactly 8 digits.
- (?!\\d):
Negative lookahead assertion to ensure the 8 digits are not followed by
another digit.
This combination ensures that the match is exactly 8 digits
and not part of a longer sequence of digits.
- Pattern
and Matcher:
- Pattern.compile(regex):
Compiles the regex into a pattern.
- pattern.matcher(input):
Creates a matcher that will match the input string against the pattern.
- Masking:
- matcher.appendReplacement(result,
mask): Appends the portion of the input string before the current match
and replaces the match with the masking string (********).
- matcher.appendTail(result):
Appends the remaining portion of the input string after the last match.
Example Output
When you run the provided code, you will see output similar
to this:
plaintext
Original:
Here are some numbers: 12345678 and 87654321. Also, 1234 and 123456789 are
here. Masked: Here are some numbers: ******** and
********. Also, 1234 and 123456789 are here. Original:
Special characters: *12345678* and #87654321#. Masked: Special characters: ********** and
#********#. Original:
Mixed in text: text12345678text and other87654321other. Masked: Mixed in text: text********text and
other********other. Original:
Digits with special characters: 12-345678 or 8765-4321. Masked: Digits with special characters:
12-******** or ********-4321. |
This shows that the 8-digit sequences are correctly masked,
even when they are adjacent to special characters.
Another one approach:
Step 1
package
com.masking; public class
MaskDemo { public static void main(String[] args) { Mask mask=new Mask(); //String data="dgdsg565656
678TT675-098765AA 7657667,6767RR5565"; String data="CAREMARK MAIL 88866RR 823
5454334DD 22 SS 7859 980 999 ssn-888-22-7227 IL 123449459580
4444-4444-5555-3333 AA 324-78787676556"; System.out.println(data); System.out.println(mask.maskValue(data)); } } |
Step 2
package
com.masking; import
java.util.regex.Matcher; import
java.util.regex.Pattern; public class
Mask { String maskValue(String data) { int[] a = new int[data.length()]; char[] ch = data.toCharArray(); int k = 0; int i = 0; for (i = 0; i < ch.length; i++) { int temp = i; if (isSpecialAndDigit(ch[i])) { int countNumber = 0; for (int j = i; j < ch.length; j++) { if (isDigit(ch[j])) { temp = j; countNumber = countNumber + 1; if (countNumber == 8) { a[k] = i; k++; a[k] = j; k++; } } else if (isSpecialChar(ch[j])) { temp = j; } else { temp = j; break; } } } else { i = temp; } i = temp; } String ccPattern =
"(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|" +
"(?:2131|1800|35\\d{3})\\d{11}|(?:4\\d{3}|5[1-5]\\d{2}|6011|7\\d{3})-?\\d{4}-?\\d{4}-?\\d{4}|3[4,7]\\d{13})"; String desc = displayMaskValue(data, a,
ch); if (desc.matches(".*" + ccPattern
+ ".*")) { Pattern compile =
Pattern.compile(ccPattern); Matcher matcher = compile.matcher(desc); matcher.find(); String masked =
matcher.group().replaceAll("[0-9]", "X"); desc = desc.replaceAll(ccPattern, masked); } return desc; } /** * @param data * @param a * @param ch * @return */ private String displayMaskValue(String data,
int[] a, char[] ch) { StringBuffer br = new StringBuffer(); int pointer = 0; for (int m = 0; m < ch.length; m++) { if (isSpecialAndDigit(ch[m])) { for (int j = pointer; j < a.length;
j++) { if (a[j] > 0 && m == a[j]) { pointer = pointer + 2; if (a[j + 1] > 0) { for (int j2 = a[j]; j2 <= a[j + 1];
j2++) { if (isDigit(data.charAt(j2))) { br.append('X'); } else { br.append(data.charAt(j2)); } } m = a[j + 1]; break; } } else { br.append(ch[m]); break; } } } else { br.append(ch[m]); } } return br.toString(); } boolean isDigit(char c) { boolean isDigitAndCharacter = (c >= '0'
&& c <= '9'); return isDigitAndCharacter; } boolean isSpecialChar(char c) { boolean isSpecial = ((c == '!') || (c ==
'@') || (c == '#') || (c == '$') || (c == '&') || (c ==
'(') || (c == ')') || (c == '\\') || (c == '-') || (c ==
'`') || (c == '+') || (c == ',') || (c == '/') || (c ==
'"') || (c == ' ')); return isSpecial; } boolean isSpecialAndDigit(char ch) { boolean isDigitAndCharacter = ((ch >=
'0' && ch <= '9') || (ch == '!') || (ch == '@') || (ch == '#') || (ch ==
'$') || (ch == '&') || (ch == '(') || (ch == ')') || (ch ==
'\\') || (ch == '-') || (ch == '`') || (ch == '+') || (ch ==
',') || (ch == '/') || (ch == '"')); return isDigitAndCharacter; } } |
Output
CAREMARK MAIL
88866RR 823 5454334DD 22 SS 7859 980 999 ssn-888-22-7227 IL
123449459580 4444-4444-5555-3333 AA 324-78787676556 CAREMARK MAIL
88866RR XXX XXXXX34DD 22 SS XXXX XXX X99 ssn-XXX-XX-XXX7 IL
XXXXXXXX9580 XXXX-XXXX-XXXX-XXXX AA XXX-XXXXX676556 |
0 Comments