My application currently logs sensitive information, which I need to mask.
A current log line looks like:
<Unable to fetch user info combination of dob=[20001231] and pan=[ABCD1234Z]
But should be changed to something like
<Unable to fetch user info combination of dob=******** and pan=********>
I tried to mask this using
str.replaceAll("\\[.*?\\]", "*")
but it changed it to:
<Unable to fetch user info combination of dob=* and pan=*>
How can I preserve the quantity of characters when masking characters between square brackets?
$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1. 1* means any number of ones.
For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.
It can be done in one line:
str = str.replaceAll("(?=[^\\[]+]).", "*");
See live regex demo and/or live Java demo.
This preserves the square brackets. To omit them from the result, use this:
str = str.replaceAll("\\[?(?=[^\\[]*]).]?", "*");
See live Java demo.
You can manipulate Pattern and Matcher to do this. For example like this:
String log = "<Unable to fetch user info combination of dob=[20001231] and pan=[ABCD1234Z]>";
Pattern pattern = Pattern.compile("\\[.*?\\]");
Matcher matcher = pattern.matcher(log);
String match="";
while (matcher.find()){
match=matcher.group();
char[] symbols = new char[match.length()];
Arrays.fill(symbols, '*');
log = log.replace(match, new String(symbols));
}
System.out.println(log);
Output:
<Unable to fetch user info combination of dob=******** and pan=********>
There might be some performance issue in the example above, but at least you got the idea.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With