I have a string containing several parameters, e.g.
PARAM1="someValue", PARAM2="someOtherValue"...
For log-output I want to "hide" some of the parameter's values, i.e. replace them with ***.
I use the following regex to match the parameter value, which works fine for most cases:
(PARMANAME=")[\w\s]*"
However, this regex only matches word- and whitespace-characters. I want to extend it to match all characters between the two quotation marks. The problem is, that the value itself can contain (escaped) quotation marks, e.g.:
PARAM="the name of this param is \"param\""
How can I match (and replace) that correctly?
My Java-method looks like this:
/**
* @param input input string
* @param params list of parameters to hide
* @return string with the value of the parameter being replace by ***
*/
public static String hideParamValue(String input, final String... params)
{
for (String param : params)
{
input = input.replaceAll("(" + param + "=)\\\"[\\w\\s]*\\\"", "$1***");
}
return input;
}