I would like to obscure certain password type words within a json string, such that the following:
"password":"foobar1"
would be replaced with
"password":"XXXXX"
or
"pwd":"foobar1"
would be replaced with
"pwd":"XXXXX"
I was able to do it using the following Pattern and replaceAll method
REGEX_JSON_PASSWORD =
Pattern.compile("\"(?i)(password|pwd)\":\"[\\w\\p{Punct}&&[^&]]*?\"");
replacementString =
REGEX_JSON_PASSWORD.matcher(returnMe).replaceAll("\"$1\":\"XXXXXXXXXXXXXXXX\"");
I don't like having to add the double quotes in the replacement string, but if I don't add them, all I get is
pwd:foobar1
(no quotes)
Is there a more efficient way of implementing the replaceAll than what is show above?