Reputation: 579
I have a string that looks like this
"He said, ""What?"""
In the entire file, there's actually more lines like that, separated by commas. The output of that line should look something like this:
He said, "What?!!"
I'm trying to do that by using this method:
Pattern pattern = Pattern.compile("\\s*(\"[^\"]*\"|[^,]*)\\s*");
Matcher matcher = pattern.matcher(line);
while (matcher.find())
{
System.out.println(matcher.group(1));
lines.add(matcher.group(1)); //adds each line to an arraylist
}
However, the output I'm getting is this:
He said,
What?
I'm pretty sure the cause is with my regular expressions since all this does is remove all the double quotes.
Upvotes: 0
Views: 810
Reputation: 1851
The process of forming quoted string is:
The code below just reverses this process:
It first removes the outer double quotes, then un-escapes the inner double quotes, and then splits:
public static void main(String[] args) {
String input = "\"He said, \"\"What?\"\"\"";
String[] out = input.replaceAll("^(\")|(\")$", "").replace("\"\"", "\"").split(", ");
for (String o : out) {
System.out.println(o);
}
}
Output:
He said
"What?"
Upvotes: 0
Reputation: 30273
It's because your regular expression matches
"He said, "
then
"What?"
then
""
It seems like what you actually want is to remove one level of double-quotes. To do that, you need to use lookaround assertions:
Pattern pattern = Pattern.compile("\\s*\"(?!\")[^\"]*(?<!\")\"\\s*");
Upvotes: 1
Reputation: 208974
Why not just use String#replaceAll
line.replaceAll("\"", "");
Upvotes: 1