0

I have a method that reads a sql query as a string and needs to replace all '\' with '\\'. This is done to prevent \n and \r being processed as line breaks in the output.

private static String cleanTokenForCsv(String inputToken) {
        if (inputToken == null) {
            return "";
        }
        if (inputToken.length() == 0 || inputToken.contains(",") || inputToken.contains("\"")
                || inputToken.contains("\n") || inputToken.contains("\r")) {
            String replacedToken = inputToken.replace(",", ";");
            return String.format("\"%s\"", replacedToken.replace("\"", "\"\""));
        } else {
            return inputToken;
        }
    }

Sample Input

(\nSELECT\n a.population_id\n ,a.empi_id\n\r ,a.encounter_id\n ,SPLIT_PART(MIN(a.service_date||'|'||a.norm_numeric_value),'|',2)::FLOAT as earliest_temperature\nFROM\n ph_f_result a)

The expected output for the query would be along the lines of

"(\nSELECT\n a.population_id\n ;a.empi_id\n\r ;a.encounter_id\n ;SPLIT_PART(MIN(a.service_date||'|'||a.norm_numeric_value);'|';2)::FLOAT as earliest_temperature\nFROM\n ph_f_result a)"

The entire query in one line with the line breaks intact

However, the output instead is

"(
SELECT
    a.population_id
    ;a.empi_id

    ;a.encounter_id
    ;SPLIT_PART(MIN(a.service_date||'|'||a.norm_numeric_value);'|';2)::FLOAT as earliest_temperature
FROM
    ph_f_result a)"

I also tried the following:

replacedToken = replacedToken.replace("\\", "\\\\");

With Regex

replacedToken = replacedToken.replaceAll("\\\\", "\\\\\\\\");

Edit: So I can get it to work if I add individual replace calls for \n and \r like below

replacedToken = replacedToken.replace("\n","\\n"); 
replacedToken = replacedToken.replace("\r", "\\r");

But I am looking for something more generic for all '\n' instances

4
  • 2
    query.replaceAll("\n", "\\n") etc. Commented Mar 24, 2022 at 14:35
  • There's also "\r" present in the input Commented Mar 24, 2022 at 14:36
  • 1
    Please show an MCVE that especially shows the precise characters in your string. Commented Mar 24, 2022 at 14:43
  • 1
    @AndyTurner There's no need for regular expressions here. Commented Mar 24, 2022 at 15:44

2 Answers 2

1

You have carriage return and newline characters, not escaped r or n.

replacedToken = replacedToken.replace("\n", "\\n").replace("\r", "\\r");

This replaces all carriage return and newline characters with their escaped equivalents.

Sign up to request clarification or add additional context in comments.

Comments

1

I assume that your goal is simply to convert characters \r, \t and \n in an input String to double-quoted two-character strings "\\r" and so on, so that printing the string does not result in newlines or tabs.

Note that the character \n does not really contain the character \ at all. We simply agree to write \n to represent it. This should work:

   public static String escapeWhitespaceEscapes(String input) {
       return input
           .replace("\n", "\\n")
           .replace("\r", "\\r")
           .replace("\t", "\\t");
   }

But note that you will have to perform the reverse operation to get back the original string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.