Reputation: 1
I have a String with outer double quotes ""
. There are inner double quotes that I need to remove. What is a regular expression for this?
For example:
input: "Hello there "I arrive" tonight" ---> output: "Hello there I arrive tonight"
input: "Hello there "I arrive tonight"" ---> output: "Hello there I arrive tonight"
input: ""Hello" there I arrive tonight" ---> output: "Hello there I arrive tonight"
I tried the following code, but it doesn't work with my examples 2 and 3. It works for example 1.
data.replaceAll("\"(\\b[^\"]+)?\"\\b([^\"]+)\\b\"([^\"]+\\b)?\"","\"$1$2$3\"");
Background: I have a CSV file, which I need to parse. The data comes with inner quotes inside the outer quotes. For example:
"aa","bb","cc","dd "REMOVE QUOTES" "
"aaa","bbb","ccc",""REMOVE QUOTES" ddd "
I would like the Regexp to remove the inner quotes only and keep the outer ones. Output:
"aa","bb","cc","dd REMOVE QUOTES "
"aaa","bbb","ccc","REMOVE QUOTES ddd "
Upvotes: 0
Views: 2243
Reputation: 159754
You could use a combination of negative look-behind and look-ahead:
data = data.replaceAll("(?<!^)\"(?!$)", "")
(?<!^)
Negative look-behind for start of line(?!$)
Negative look-ahead for EOL
Upvotes: 2
Reputation: 89557
If I suppose that your strings begin with the first " and finish with the last ", you can use this for a regex way:
data.replaceAll("(?<!^)\"(?!$)", "");
Upvotes: 0
Reputation: 2773
Give this a shot. I get the inside string (ignoring the outer quotes) then remove all quotes and add the outer ones back.
String input1 = "\"Hello there \"I arrive\" tonight\"";
String output1 = "\"" + input1.substring(1, input1.length() - 1).replaceAll("\"", "") + "\"";
System.out.println(output1);
Ouput
"Hello there I arrive tonight"
Upvotes: 1