Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Escaped quotes are a real PITA in Java, but this should do the trick:</p> <pre><code>public class Test { public static String hideParamValue(String input, final String... params) { for (String param : params) { input = input.replaceAll( "(" + param + "=)\"(?:[^\"\\\\]|\\\\.)*\"", "$1***"); } return input; } public static void main(String[] args) { String s = "PARAM1=\"a b c\", PARAM2=\"d \\\"e\\\" f\", PARAM3=\"g h i\""; System.out.println(s); System.out.println(hideParamValue(s, "PARAM2", "PARAM3")); } } </code></pre> <p>output:</p> <pre><code>PARAM1="a b c", PARAM2="d \"e\" f", PARAM3="g h i" PARAM1="a b c", PARAM2=***, PARAM3=*** </code></pre> <p><code>[^\"\\\\]</code> matches any one character other than a quotation mark or a backslash. The backslash has to be escaped with another backslash for the regex, then each of those has to be escaped for the string literal. But the quotation mark has no special meaning in a regex, so it only needs one backslash.</p> <p><code>(?:[^\"\\\\]|\\\\.)</code> matches anything except a quotation mark or a backslash, OR a backslash followed by anything. That takes care of your escaped quotation marks, and also allows for escaped backslashes and other escape sequences, at no extra cost. </p> <p>The negative-lookbehind approach suggested by @axtavt only handles escaped quotes, and it treats <code>\\"</code> as a backslash followed by an escaped quote, when it was probably intended as an escaped backslash followed by a quote.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload